0

i've been doing while loops for this problem .. i dont think its the right way though.

i have a set of boxes each with a boxid(apart from the first), which is the id(mysql) of the previous box.

e.g.
box 1 has id 1
box 2 has id 2 and boxid 1,
box 3 has id 3 and boxid 2 etc

you see the boxid is the same as the previous id.

i want 10 boxes to display , when i'm trying to loop this i am using a while loop, but i keep on having to use another while loop inside, just to match the boxid with previous id to get the next box to display. so i would have to do 10 while loops inside one another, which isn't good practice.

is there a better way to do this? for loop?

another example

e.g.
box 1 has id 239
box 2 has id 456 and boxid 239,
box 3 has id 123 and boxid 456 etc

4
Contributors
7
Replies
8
Views
6 Years
Discussion Span
Last Post by diafol
0

i wish to help u with this but im having a bit of difficulty understanding ur problem.

i've been doing while loops for this problem

what exactly is 'this problem'?
are u trying to retrieve a set of 'boxes' from a mysql database and arrange them in order?

Edited by kekkaishi: n/a

0

you want something like this:

$i=1;
   while($i<10)
   {
	   if($i==1)
	   {
		   print "id : "+$i;
		   echo "<br>";
	   }
	   else
	   {
		   echo "id : "+$i+" , ";	
		   echo "boxid : "+($i-1);
		   echo "<br>";
	   }
	   $i++;
   }
0

you want something like this:

$i=1;
   while($i<10)
   {
	   if($i==1)
	   {
		   print "id : "+$i;
		   echo "<br>";
	   }
	   else
	   {
		   echo "id : "+$i+" , ";	
		   echo "boxid : "+($i-1);
		   echo "<br>";
	   }
	   $i++;
   }

i wanted to suggest this method but the other example provided stopped me from doing so.

box 1 has id 239
box 2 has id 456 and boxid 239,
box 3 has id 123 and boxid 456

here the id is random. it is not a consecutively incrementing number. thats y i asked if this is a problem concerning arranging a set of boxes by of course referring to boxId...

0

thanks for the help but the id's are random, they come from mysql. so subtracting 1, won't help.

e.g.
[id=239] [id=456,boxid=239][id=123,boxid=456] etc

would an array be the best way to do this? because i can't keep on doing while loops inside of while loops to get the previous id.

0
$getrest =  mysql_query("SELECT * FROM block WHERE sid='$id' ORDER BY `id` DESC",$this->connect);

while($row1 = mysql_fetch_assoc($getrest)) {

$idz = $row1['id'];

$getmore =  mysql_query("SELECT * FROM block WHERE sid='$idz' ORDER BY `id` DESC",$this->connect);					
							 while ($row2 = mysql_fetch_assoc($getmore)) {

}

}

you see i'm using the result from the first query in the where clause of the second query. This isn't good practice as what i'm trying to accomplish could lead me doing this up to ten times. what would be a better way to do this than while loops inside of while loops?


example?

Edited by MDanz: n/a

0

maybe this could work for ye.

$sql = "SELECT * FROM blocks WHERE sid={$id}";
    $res = mysql_query($sql);
    $row = mysql_fetch_array($res);
    
    $bid = $row['sid'];
    echo $row['sid']."<br />";
    $x = true;
    while($x = true){ //use a boolean to continue loop
        $sql = mysql_query("SELECT * FROM blocks WHERE id= {$bid}");
        if(mysql_num_rows($sql)==0){
            $x=false; //if there is no box with id of the previous box then the result set is null. so this is the end of the link betn the boxes. so make $x false and break the loop. 
            break;
        }else{
            $row = mysql_fetch_array($res);
            echo "box id: ". $row['sid']." and Id: ".$row['id']."<br />";
            $bid = $row['sid']; //always updating $bid so that when the loop starts again $bid is a new one and the query within the loop always selects a new result
        }
    }

hope this helps

0

Has anybody asked "why?"

If the boxes are in order, just use the previous ID?

Although I understand the problem, it doesn't seem like a rational thing to do.

$r = mysql_query("SELECT id FROM boxes");
$prev="";$x = 1;$output="";
while($d = mysql_fetch_array($r)){
  $output = "box $x has id = {$d['id']}";
  if($prev != "")$output .= " and box_id = $prev";
  $output .= "<br />";
  $prev = $d['id'];
  $x=$x+1;
}
...
echo $output;

Edited by diafol: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.