Jump to content

[SOLVED] Problem with "$row_count++"


DBookatay

Recommended Posts

Can anyone spot a problem with the following code?

My line

if (!$row_count) $listings = '<div><b>Sorry, no listings under '.$category.' matchining your criteria were found.</b></div>';

is not working.

 

Complete code:

$query = "SELECT * FROM listings WHERE category = '{$_GET['id']}'";
$result = mysql_query($query); 
$numrows = mysql_num_rows($result); 
for($x = 0; $row = mysql_fetch_array($result); $x++) {

	$id = $row['id'];
	$lines = stripslashes($row['lines']);
	$markets = stripslashes($row['markets']);
	$opportunity = stripslashes($row['opportunity']);

	$listings .= '<div id="list"><table width="100%"><tr><td class="listID"><a href="NEED_LINK?id='.$id.'">Listing ID: '.$id.'</a></td></tr><tr><td class="listLine"><b>Product Lines:</b> '.$lines.'</td></tr><tr><td class="listLine"><b>Target Markets:</b> '.$markets.'</td></tr><tr><td class="listLine"><b>Sales Opportunity:</b> '.$opportunity.'</td></tr></table></div>'; $row_count++;

	if (!$row_count) $listings = '<div><b>Sorry, no listings under '.$category.' matchining your criteria were found.</b></div>';
}

Link to comment
https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/
Share on other sites

$query = "SELECT * FROM listings WHERE category = '{$_GET['id']}'";

$result = mysql_query($query);

$numrows = mysql_num_rows($result);

for($x = 0; $row = mysql_fetch_array($result); $x++) {

 

$id = $row['id'];

$lines = stripslashes($row['lines']);

$markets = stripslashes($row['markets']);

$opportunity = stripslashes($row['opportunity']);

 

$listings .= '<div id="list"><table width="100%"><tr><td class="listID"><a href="NEED_LINK?id='.$id.'">Listing ID: '.$id.'</a></td></tr><tr><td class="listLine"><b>Product Lines:</b> '.$lines.'</td></tr><tr><td class="listLine"><b>Target Markets:</b> '.$markets.'</td></tr><tr><td class="listLine"><b>Sales Opportunity:</b> '.$opportunity.'</td></tr></table></div>'; $row_count++;

 

if (!$numrows)

{

$listings = '<div><b>Sorry, no listings under '.$category.' matchining your criteria were found.</b></div>';

}

}

Sorry didnt look at the code good the first time.

$query = "SELECT * FROM listings WHERE category = '{$_GET['id']}'";
   $result = mysql_query($query); 
   $numrows = mysql_num_rows($result); 
$row_count = "0";
   for($x = 0; $row = mysql_fetch_array($result); $x++) {

      $id = $row['id'];
      $lines = stripslashes($row['lines']);
      $markets = stripslashes($row['markets']);
      $opportunity = stripslashes($row['opportunity']);

      $listings .= '<div id="list"><table width="100%"><tr><td class="listID"><a href="NEED_LINK?id='.$id.'">Listing ID: '.$id.'[/url]</td></tr><tr><td class="listLine">Product Lines: '.$lines.'</td></tr><tr><td class="listLine">Target Markets: '.$markets.'</td></tr><tr><td class="listLine">Sales Opportunity: '.$opportunity.'</td></tr></table></div>'; 
$row_count++;

      if (!$numrows == "0")
{
$listings = '<div>Sorry, no listings under '.$category.' matchining your criteria were found.</div>';
   }
}

Problem is $row_count++; is always adding 1 to it, whether there is anything or not.  Atlanta was on the right track:

 

$query = "SELECT * FROM listings WHERE category = '{$_GET['id']}'";

$result = mysql_query($query);

$numrows = mysql_num_rows($result);

for($x = 0; $row = mysql_fetch_array($result); $x++) {

 

     $id = $row['id'];

     $lines = stripslashes($row['lines']);

     $markets = stripslashes($row['markets']);

     $opportunity = stripslashes($row['opportunity']);

 

     $listings .= '<div id="list"><table width="100%"><tr><td class="listID"><a href="NEED_LINK?id='.$id.'">Listing ID: '.$id.'[/url]</td></tr><tr><td class="listLine">Product Lines: '.$lines.'</td></tr><tr><td class="listLine">Target Markets: '.$markets.'</td></tr><tr><td class="listLine">Sales Opportunity: '.$opportunity.'</td></tr></table></div>';

}

 

if (!$numrows) {

$listings = '<div>Sorry, no listings under '.$category.' matchining your criteria were found.</div>';

}  

Here's a more important question: why use $row_count at all?  You're already using mysql_num_rows().  Why not just go:

 

<?php
   $query = "SELECT * FROM listings WHERE category = '{$_GET['id']}'";
   $result = mysql_query($query);
   $numrows = mysql_num_rows($result);

   if($numrows){
      while($row = mysql_fetch_assoc($result)){
         $id = $row['id'];
         $lines = stripslashes($row['lines']);
         $markets = stripslashes($row['markets']);
         $opportunity = stripslashes($row['opportunity']);

         $listings .= '<div id="list"><table width="100%"><tr><td class="listID"><a href="NEED_LINK?id='.$id.'">Listing ID: '.$id.'[/url]</td></tr><tr><td class="listLine">Product Lines: '.$lines.'</td></tr><tr><td class="listLine">Target Markets: '.$markets.'</td></tr><tr><td class="listLine">Sales Opportunity: '.$opportunity.'</td></tr></table></div>';
      }
   }
   else{
      $listings = '<div>Sorry, no listings under '.$category.' matchining your criteria were found.</div>';
   }
?>

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.