d3vilr3d Posted October 1, 2007 Share Posted October 1, 2007 Hi all, I'm pretty new to PHP and is trying to edit an existing listing script. What I am trying to do is to show a number of listing per sub categories on the page. But I am having a hard time pulling out the data and display the count. Heres the code I have so far. Please help me. thanks so much in advance. <?PHP if (!defined("MC_KEY")) die("Security Error."); include_once('includes/database.php'); require_once('config.php'); if ($_REQUEST[action]=="showreg" || !isset($_REQUEST[action])) { $categories = $info->getCategories(); // INCLUDE REGIONID IN URL, IF NECESSARY if (isset($_REQUEST[regionid])) $region_req = "®ionid=".$_REQUEST[regionid]; for ($i = 0; $i < sizeof($categories); $i++){ $url = "?action=showads$region_req&categoryid=".$categories[$i][id]; echo "<TABLE class='list_categories'>"; echo "<TR valign='top'>"; echo "<TD>"; echo "<div class='main_category_name'>".$categories[$i][name]."</div>\n"; echo "</TD>"; echo "<TD>"; $sub_categories = $info->getSubCategories($categories[$i][id]); $res = sprintf("SELECT * FROM `ad` WHERE `sub_category_id` = .$sub_categories"); $result = array($res); $count = count($result); echo "<TABLE class='category_table_$site_default_lang'>\n"; echo "<TR>\n"; for ($j = 0; $j < sizeof($sub_categories); $j++) { $url = "?action=showads$region_req&categoryid=".$categories[$i][id]."&subcategoryid=".$sub_categories[$j][id]; if ($j%5 == 0) echo "<TD class='category_column'>"; echo "<A href='$url'>".$sub_categories[$j][name]." ".$count."[/url]<BR/>\n"; } echo "</TR>\n"; echo "</TABLE>"; echo "</TD>"; echo "</TR>"; echo "<TR><TD> </TD><TD><hr style='height:1px; color: #ccc;width:420px;'/></TD></TR>"; echo "</TABLE>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/ Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 <?php $sub_categories = $info->getSubCategories($categories[$i][id]); $res = sprintf("SELECT * FROM `ad` WHERE `sub_category_id` = .$sub_categories"); $result = mysql_query($res)or die(mysql_error()); $count = mysql_num_rows($result); ?> Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359621 Share on other sites More sharing options...
marcus Posted October 1, 2007 Share Posted October 1, 2007 Why was my post deleted o.O Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359623 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 Why was my post deleted o.O I never saw it...maybe you accidentally posted it on a different topic? Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359625 Share on other sites More sharing options...
d3vilr3d Posted October 1, 2007 Author Share Posted October 1, 2007 sorry, I think i double posted... Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359627 Share on other sites More sharing options...
marcus Posted October 1, 2007 Share Posted October 1, 2007 Hmm, odd, but Poco's solution should do the trick. Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359628 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 This is crazy, it's like every time someone posts, some of the other ones disappear. Hopefully it's only this topic... Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359635 Share on other sites More sharing options...
d3vilr3d Posted October 1, 2007 Author Share Posted October 1, 2007 Hi Again, I inserted the code above and received: You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 >.< without the or die, error is: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/mandarin/public_html/tiles/list_categories.php on line 29 Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359641 Share on other sites More sharing options...
d3vilr3d Posted October 1, 2007 Author Share Posted October 1, 2007 Here's my code again, with code tag, sorry about that... <?PHP if (!defined("MC_KEY")) die("Security Error."); include_once('includes/database.php'); require_once('config.php'); if ($_REQUEST[action]=="showreg" || !isset($_REQUEST[action])) { $categories = $info->getCategories(); // INCLUDE REGIONID IN URL, IF NECESSARY if (isset($_REQUEST[regionid])) $region_req = "®ionid=".$_REQUEST[regionid]; for ($i = 0; $i < sizeof($categories); $i++){ $url = "?action=showads$region_req&categoryid=".$categories[$i][id]; echo "<TABLE class='list_categories'>"; echo "<TR valign='top'>"; echo "<TD>"; echo "<div class='main_category_name'>".$categories[$i][name]."</div>\n"; echo "</TD>"; echo "<TD>"; $sub_categories = $info->getSubCategories($categories[$i][id]); $res = sprintf("SELECT * FROM `ad` WHERE `sub_category_id` = .$sub_categories"); $result = mysql_query($res); $count = mysql_num_rows($result); echo "<TABLE class='category_table_$site_default_lang'>\n"; echo "<TR>\n"; for ($j = 0; $j < sizeof($sub_categories); $j++) { $url = "?action=showads$region_req&categoryid=".$categories[$i][id]."&subcategoryid=".$sub_categories[$j][id]; if ($j%5 == 0) echo "<TD class='category_column'>"; echo "<A href='$url'>".$sub_categories[$j][name]. ".$count.</A><BR/>\n"; } echo "</TR>\n"; echo "</TABLE>"; echo "</TD>"; echo "</TR>"; echo "<TR><TD> </TD><TD><hr style='height:1px; color: #ccc;width:420px;'/></TD></TR>"; echo "</TABLE>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359649 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 Change this line: $result = mysql_query($res); To $result = mysql_query($res)or die(mysql_error()); Tell us what you get as an error. Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359651 Share on other sites More sharing options...
d3vilr3d Posted October 1, 2007 Author Share Posted October 1, 2007 Change this line: $result = mysql_query($res); To $result = mysql_query($res)or die(mysql_error()); Tell us what you get as an error. i get You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359652 Share on other sites More sharing options...
pocobueno1388 Posted October 1, 2007 Share Posted October 1, 2007 Try this <?php $sub_categories = $info->getSubCategories($categories[$i][id]); $sub_categories = mysql_real_escape_string($sub_categories); $res = sprintf("SELECT * FROM `ad` WHERE `sub_category_id` = .$sub_categories"); $result = mysql_query($res); ?> If that doesn't work, echo $sub_categories out and tell us what it displays. Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359659 Share on other sites More sharing options...
d3vilr3d Posted October 1, 2007 Author Share Posted October 1, 2007 hmm i see... $sub_categories echos Array. hmm... Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-359674 Share on other sites More sharing options...
pocobueno1388 Posted October 2, 2007 Share Posted October 2, 2007 To see whats in the array, just do print_r($sub_categories); After that...your going to have to explain what your wanting to do. Your either going to need to pick which value in the array you want, or you are going to want to loop through each one of them with your query. Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-360102 Share on other sites More sharing options...
d3vilr3d Posted October 3, 2007 Author Share Posted October 3, 2007 Hi again, Thanks so much for your pointer on the print_r. I got the count pulled up. Heres what I did: $res = sprintf("SELECT * FROM `ad` WHERE `sub_category_id` = '{$sub_categories[$j][id]}'"); $result = mysql_query($res)or die(mysql_error()); $count = mysql_num_rows($result); echo "$count"; How does this code look, clean? It gets the job done but I dont know if its good coding Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-361069 Share on other sites More sharing options...
MmmVomit Posted October 3, 2007 Share Posted October 3, 2007 You're not using sprintf correctly. What you have will work, but it doesn't make much sense. You want either one of these. $res = sprintf("SELECT * FROM `ad` WHERE `sub_category_id` = '%s'", $sub_categories[$j]['id']); $res = "SELECT * FROM `ad` WHERE `sub_category_id` = '{$sub_categories[$j][id]}'"; Quote Link to comment https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/#findComment-361083 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.