Jump to content

loop only grabs first result & displays repeatedly


doa24uk

Recommended Posts

Hi all,

my loop is now working & outputting correctly, HOWEVER it is only grabbing the first result and repeatring it.


I've narrrowed it down to the fact that $j is resetting to 0 so each time it resets it picks up teh first result again. Am I right in thinking this?

If you could have a look that'd be great!


[code]<?

$query="SELECT * FROM *** ORDER BY price DESC LIMIT 50";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

// logic



$i = 0;
$j = 0;
echo "<tr>";
while ($i < $num) {
  $i++;
  $j++;
  echo "<td><a href=".$url." Alt=".$ltitle.">".$ltitle."</a><br>".$ldesc."<br><b>&pound; ".$price."</td>";
  //if $j equals 5, then start a new row and reset $j to zero
  //let $i keep counting the current row.
  if ($j == 5) {
      $j = 0;
      echo "</tr><tr>";

echo "</tr>";
}
}
?>
[/code]

It's driving me nuts!


Many thanks


DoA
You're not fetching any records from the database at all, so I'm surprised you're getting any records displayed.

Try something like this:
[code]<?php
$query="SELECT * FROM *** ORDER BY price DESC LIMIT 50";
$result=mysql_query($query);

$num=mysql_numrows($result);

// logic

$j = 0;
echo "<tr>";
while (row = mysql_fetch_assoc($result)) {
  $j++;
  echo "<td><a href=".$row['url']." Alt=".$row['ltitle'].">".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>&pound; ".$row['price']."</td>";
  //if $j equals 5, then start a new row and reset $j to zero
  //let $i keep counting the current row.
  if ($j == 5) {
      $j = 0;
      echo "</tr><tr>";

echo "</tr>";
}
}?>[/code]

Ken
Sorry guys, no luck - this code gives

Parse error: syntax error, unexpected '=' in /home/lazyrec/public_html/index.php on line 47

[code]<?php
$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";
$result=mysql_query($query);

$num=mysql_num_rows($result);

mysql_close();

$buid = mysql_result($result,$i,"buid");
$ltitle = mysql_result($result,$i,"ltitle");
$ldesc = mysql_result($result,$i,"ldesc");
$url = mysql_result($result,$i,"url");
$price = mysql_result($result,$i,"price");

$i = 0;
$j = 0;

echo "<tr>";
while (row = mysql_fetch_assoc($result)) {
  $i++
  $j++;
  echo "<td><a href=".$row['url']." Alt=".$row['ltitle'].">".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>&pound; ".$row['price']."</td>";
  //if $j equals 5, then start a new row and reset $j to zero
  //let $i keep counting the current row.
  if ($j == 5) {
      $j = 0;
      echo "</tr><tr>";

echo "</tr>";
}
}
?>[/code]

Line 47 is the blank line before this one -

[code]$buid = mysql_result($result,$i,"buid");[/code]

DoA

p.s. I've also tried without the spaces before & after the =
If you use my suggestion, you don't need the lines:
[code]<?php
mysql_close();

$buid = mysql_result($result,$i,"buid");
$ltitle = mysql_result($result,$i,"ltitle");
$ldesc = mysql_result($result,$i,"ldesc");
$url = mysql_result($result,$i,"url");
$price = mysql_result($result,$i,"price");

$i = 0;
?>[/code]

You don't need the variable [b]$i[/b] at all.

Ken
[code=php:0]
<?php
$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$j = 0;
echo "<tr>";
while ($row = mysql_fetch_assoc($result)) {
  $i++
  $j++;
  echo "<td><a href='".$row['url']."' Alt='".$row['ltitle']."'>".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>&pound; ".$row['price']."</td>";
  //if $j equals 5, then start a new row and reset $j to zero
  //let $i keep counting the current row.
  if ($j == 5) {
    $j = 0;
    echo "</tr><tr>";
    echo "</tr>";
  }
}
?>
[/code]
[quote author=thorpe link=topic=113360.msg460578#msg460578 date=1162308814]
[code=php:0]
<?php
$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";
$result=mysql_query($query);
$num=mysql_num_rows($result);
$j = 0;
echo "<tr>";
while ($row = mysql_fetch_assoc($result)) {
  $i++
  $j++;
  echo "<td><a href='".$row['url']."' Alt='".$row['ltitle']."'>".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>&pound; ".$row['price']."</td>";
  //if $j equals 5, then start a new row and reset $j to zero
  //let $i keep counting the current row.
  if ($j == 5) {
    $j = 0;
    echo "</tr><tr>";
    echo "</tr>";
  }
}
?>
[/code]
[/quote]

none of these are working - unexpected = 's and undexpected T_VARIABLES each time


Here's the code I'm currently running.

[code]
<?php
$query="SELECT * FROM bids ORDER BY price DESC LIMIT 50";
$result=mysql_query($query);
$num=mysql_num_rows($result);

$j = 0;
echo "<tr>";
while ($row = mysql_fetch_assoc($result)) {
  $j++;
  echo "<td><a href=".$row['url']." Alt=".$row['ltitle'].">".$row['ltitle']."</a><br>".$row['ldesc']."<br><b>&pound; ".$row['price']."</td>";
  //if $j equals 5, then start a new row and reset $j to zero
  //let $i keep counting the current row.
  if ($j == 5) {
    $j = 0;
    echo "</tr><tr>";
    echo "</tr>";
  }
}
?>
[/code]


EDIT - I REMOVED THE $i++ in the code and it appears to be working brilliantly!


hehe thank you so much!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.