Jump to content

While Loop Help - MySQL


itsureboy

Recommended Posts

Ok Heres my problem. I am fetching data from my database and displaying 1 result per table

 

for example:

 

1 result(<-looping this)

2 result

3 result

 

 

What i want to do is display 2 results in a table and loop that for example:

 

1 result - 2 result(<-looping this)

3 result - 4 result

5 result - 6 result

 

the problem is the variable will be assinged the same thing so i wouldnt be able to do that.

 

What i want to know is how i would be able to do what i said above.

 

Sorry if this doesnt make sense....

 

 

Here is my code:

 

<?php

include 'exampleconnect.php';

if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}

$max_results = 12;

$cat = $cat;

$from = (($page * $max_results) - $max_results); 

$sql = mysql_query("SELECT title, image FROM rap WHERE category = '$cat' ORDER BY id DESC  LIMIT $from, $max_results");

while($row = mysql_fetch_array($sql)){
    

echo $row['title'];

echo $row['image'];


}

include 'exampleclose.php';

 

Thanks...

Link to comment
Share on other sites

Lol ok...

 

For example I am displaying 1 product per table on my website thats being looped displaying a bunch of tables displayed vertically.

What I want to do know is embed 2 tables in a table (two products in that main table) and loop that. But if i do that it will display the same product(2 products)  in that *Main Table* because the variable is assigned the same value. So in that loop i want to make the variables switch to the next row in the same loop. Hopes this is better.

Link to comment
Share on other sites

just an idea

while($row = mysql_fetch_array($sql)){

echo $row['title'];

echo $row['image'];

if (put the condition here)

{ //put here another query if thats what you mean

while($row2 = mysql_fetch_array($sql2)){

//do somwthing here

}

}

 

}

 

Link to comment
Share on other sites

try something like this

<?php
include 'db.php';

define ("NUMCOLS",2);

$res = mysql_query("SELECT col1, col2 FROM mytable");

$count = 0;
echo "<TABLE border=1>";
while (list($col1, $col2) = mysql_fetch_row($res)) {

    if ($count % NUMCOLS == 0) echo "<TR>\n";  # new row

    echo "<TD>$col1<br>$col2</TD>\n";
    $count++;

    if ($count % NUMCOLS == 0) echo "</TR>\n";  # end row
}

# end row if not already ended

if ($count % NUMCOLS != 0) {
   while ($count++ % NUMCOLS) echo "<td> </td>";
   echo "</TR>\n";
}
echo "</TABLE>";

?>

Link to comment
Share on other sites

I tried both of those and i did'nt work....

 

I'll give it one more shot at explaining it.

 

(INFO IS IN THE CODE BOX)

<?php

include 'exampleconnect.php';

if(!isset($_GET['page'])){    $page = 1;
} else {
    $page = $_GET['page'];
}
$max_results = 12;
$cat = $cat;
$from = (($page * $max_results) - $max_results); 
$sql = mysql_query("SELECT title, image FROM rap WHERE category = '$cat' ORDER BY id DESC  LIMIT $from, $max_results");
while($row = mysql_fetch_array($sql)){
    
echo $row['title'];
echo $row['image'];

##I want to use $row['image'] again in this loop but with the value of the next row in the database (same query)

}

include 'exampleclose.php';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.