Jump to content

Recommended Posts

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 = "&regionid=".$_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>";

        }

}

?>

Link to comment
https://forums.phpfreaks.com/topic/71443-need-help-displaying-record-count/
Share on other sites

<?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);
      
?>

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

 

 

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 = "&regionid=".$_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>";
        }
}
?>

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

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.

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.

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!

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]}'";

 

 

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.