DBookatay Posted November 6, 2007 Share Posted November 6, 2007 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/ Share on other sites More sharing options...
atlanta Posted November 6, 2007 Share Posted November 6, 2007 $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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385921 Share on other sites More sharing options...
DBookatay Posted November 6, 2007 Author Share Posted November 6, 2007 Still now showing the "Sorry, no listings" line... Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385924 Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Share Posted November 6, 2007 Do you define $row_count anywhere? You should set it to 0 before your for-loop. Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385945 Share on other sites More sharing options...
atlanta Posted November 6, 2007 Share Posted November 6, 2007 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>'; } } Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385948 Share on other sites More sharing options...
fanfavorite Posted November 6, 2007 Share Posted November 6, 2007 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385954 Share on other sites More sharing options...
DBookatay Posted November 6, 2007 Author Share Posted November 6, 2007 Thanks guys, that solved it. Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385957 Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Share Posted November 6, 2007 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385959 Share on other sites More sharing options...
atlanta Posted November 6, 2007 Share Posted November 6, 2007 yea good idea i knew it was something wrong with that .. thanks for the correction. Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-385962 Share on other sites More sharing options...
DBookatay Posted November 6, 2007 Author Share Posted November 6, 2007 Here's a more important question: why use $row_count at all? You're already using mysql_num_rows(). Good point... Quote Link to comment https://forums.phpfreaks.com/topic/76247-solved-problem-with-row_count/#findComment-386007 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.