Forum Moderators: coopster

Message Too Old, No Replies

While loop with SQL query

         

dougmcc1

12:46 am on Dec 6, 2003 (gmt 0)

10+ Year Member



I'm building a directory with PHP/MySQL and I'm hung up on a while loop. Each page uses includes. When I delete a subcategory, I want to delete all the includes for that category as well as all the includes for the subcategories in that subcategory.

Each subcategory is stored in a table with the following fields:
id, section, category, previous_subcategory, subcategory, rank

A subcategory named 'state_parks' would be stored as:
99 ¦ recreation ¦ parks ¦ (blank) ¦ state_parks ¦ 1

A subcategory in that subcategory named 'big' would be stored as:
100 ¦ recreation ¦ parks ¦ state_parks ¦ big ¦ 2

And a subcategory in that subcategory would be stored as:
101 ¦ recreation ¦ parks ¦ big ¦ next_subcat ¦ 3

Here is the code:

$section="recreation";
$category="parks";
$subcategory="state_parks";
$rank=1;
//delete includes
while ($subcategory) {
$rank++;
$myquery="SELECT id,subcategory FROM list WHERE section='$section' AND category='$category' AND previous_subcat='$subcategory' AND rank='$rank';" . mysql_error();
$myresult=mysql_query($myquery);
while ($myrow = mysql_fetch_assoc($myresult)) {
extract($myrow);
$id = $myrow["id"];
$subcategory = $myrow["subcategory"];
} //extracted values
echo "<BR><BR>Subcategory is $subcategory and rank is $rank<BR><BR>";
$include=$id . ".htm";
$delinclude=unlink("/home/lcdinwcy/public_html/main_includes/$include");
} //no more subcategories

$subcategory never changes so I get an infinite loop. I want it to run until no more subcategories exist and all the includes are deleted. Any suggestions? Thanks.

dreamcatcher

8:26 am on Dec 6, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dougmcc1,

Try the following:


while ($myrow = mysql_fetch_array($myresult))
{

echo "<BR><BR>Subcategory is $myrow['subcategory'] and rank is $rank<BR><BR>";
$include = $myrow['id'] . ".htm";
$delinclude=unlink("/home/lcdinwcy/public_html/main_includes/$include");

} //no more subcategories

Think that might work. :)

dougmcc1

6:49 pm on Dec 6, 2003 (gmt 0)

10+ Year Member



Thanks for your reply dreamcatcher. I took your while(query) approach and got it to work.