Forum Moderators: coopster
SELECT id FROM table ORDER BY id DESC LIMIT 1
$query1 = "SELECT id FROM table ORDER BY id DESC LIMIT 1";
$result1 = mysql_query($query1);
$y = $result1;
echo $y;
for ($x=1; $x<27; $x++) {
etc....
}
If anyone could help me out it would be great!
SELECT count(*) AS number_of_rows FROM table;You're right, you can't "count" on the ID being the number of rows when you are deleting them here and there. Second, if you want to loop through all of them try retrieving all the rows first, then using a loop in PHP to control the processing.
$sql = "SELECT id FROM table ORDER BY id DESC";...or something along those lines...
$rows = mysql_query($sql);
if (mysql_num_rows($rows) > 0) {
while ($row = mysql_fetch_array($rows)) {
print $row['id'] . '<br />';
}
}
$query1 = "SELECT id FROM table ORDER BY id DESC LIMIT 1";
$result1 = mysql_query($query1) or die("Error: " . mysql_error());;
$row1 = mysql_fetch_array($result1);
$y = $row1["id"];
echo $y;
echo("<br /><br />");
echo "<ul><li><a href=index.php>HOME</a></li>";
for ($x=1; $x<$y; $x++) {
$query = "SELECT * FROM table where ID = $x and parentid = 0";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row = mysql_fetch_array($result);
echo("<li><a href=viewcat.php?catid=" . $x . ">" . $row["name"] . "</a>");
$query = "SELECT * FROM table where parentid = $x";
$subResult = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_fetch_array($subResult)!= "") {
echo("<ul>");
while($subRow = mysql_fetch_array($subResult)){
$z = $subRow["ID"];
echo("<li><a href=viewcat.php?catid=" . $subRow["ID"] . ">" . $subRow["name"] . "</a></li>");
$query = "SELECT * FROM table where parentid = $z";
$subSubResult = mysql_query($query) or die("Error: " . mysql_error());if(mysql_fetch_array($subSubResult)!= "") {
echo("<ul>");
while($subSubRow = mysql_fetch_array($subSubResult)){
echo("<li><a href=viewcat.php?catid=" . $subSubRow["ID"] . ">" . $subSubRow["name"] . "</a></li>");
} // END WHILE
echo("</ul>");
} // END IF
} // END WHILE
echo("</ul>");
} // END IF
echo("</li>");
} // END FOR!
echo "</ul>"; As you can see I'm very much a beginner finding his way by trying stuff out :D . I'm trying to get an automatically generated site map from sections and sub sections. Many thanks.
$query1 = "SELECT id FROM table ORDER BY id DESC LIMIT 1";
$result1 = mysql_query($query1) or die("Error: " . mysql_error());;
$row1 = mysql_fetch_array($result1);
$y = $row1["id"];
echo $y;
echo("<br /><br />");
echo "<ul><li><a href=index.php>HOME</a></li>";
for ($x=1; $x<=$y; $x++) {
$query = "SELECT * FROM table where ID = $x and parentid = 0";
$result = mysql_query($query) or die("Error: " . mysql_error());
$row = mysql_fetch_array($result);
$flag1 = "0";
$flag2 = "0";
$flag3 = "0";
if ($row['name']!= "" ) {
$flag1 = "1";
echo("<li><a href=viewcat.php?catid=" . $x . ">" . $row["name"] . "</a>");
}
$query = "SELECT * FROM table where parentid = $x";
$subResult = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_fetch_array($subResult)!= "") {
echo("<ul>");
$flag2 = "1";
while($subRow = mysql_fetch_array($subResult)){
$z = $subRow["ID"];
echo("<li><a href=viewcat.php?catid=" . $subRow["ID"] . ">" . $subRow["name"] . "</a></li>");
$query = "SELECT * FROM table where parentid = $z";
$subSubResult = mysql_query($query) or die("Error: " . mysql_error());
if(mysql_fetch_array($subSubResult)!= "") {
echo("<ul>");
$flag3 = "1";
while($subSubRow = mysql_fetch_array($subSubResult)){
echo("<li><a href=viewcat.php?catid=" . $subSubRow["ID"] . ">" . $subSubRow["name"] . "</a></li>");
} // END WHILE
if ($flag2 == "1") { echo("</ul>"); }
} // END IF
} // END WHILE
if ($flag1 == "1") { echo("</ul>"); }
} // END IF
echo("</li>");
} // END FOR!
echo "</ul>";
Many thanks.