Jump to content


Photo

showing number of rows


  • Please log in to reply
5 replies to this topic

#1 Evanthes

Evanthes
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 18 September 2006 - 07:48 PM

hey guys i think I have a relatively easy question.

I want to be able to see how many records for a primary key there are. I have a table that is filled with all this information. but the number of records doesnt update when i add a new one. check out my code and see what i mean:

$zone = $searchterm; // var created for link back to search
$query = "select * from tbl_sites where zone = '".$searchterm."'" ;
$result = mysql_query($query);

$num_results = mysql_num_rows($result);
echo "<center>";
echo "<br><br><a href='test.php'>Return to list of Zones</a>";
echo '<p> number of records found:' .$num_results. '</p>';
echo "<center>";
echo '<table border="1" align="center">';
echo '<tr><td>';
echo 'Site ID</td><td>Net</td><td>State</td><td>Zone</td><td>City</td><td>Address</td><td>Jobs</td>';
for ($i=0; $i <$num_results; $i++)
{
$row = mysql_fetch_array($result);
	$siteid = $row['site_id'];
	echo '<tr><td>';
echo '<tr><td>';
	echo htmlspecialchars(stripslashes($row['site_id']));
	echo '</td><td>';
	echo stripslashes($row['net']);
	echo '</td><td>';
	echo stripslashes($row['state']);
	echo '</td><td>';
	echo "</a>";
	echo stripslashes($row['zone']);
	echo '</td><td>';
	echo "</a>";
	echo stripslashes($row['city']);
	echo '</td><td>';
	echo "</a>";
	echo "<a href='siteinfo.php?searchterm=$siteid&zone=$zone'>";
	echo stripslashes($row['address']);
	echo '</td><td>';
	echo "</a>";
	echo stripslashes($row['jobs']);
	echo '</td>';	
	echo "</tr>";
this statement basically fills my table with data of each record. I'm trying to show the number of jobs and have it update when a new one is added, just like the numer of threads in a topic for a message board like this. although its not working, im guessing i need some sort of mysql_num_rows statement but i guess i dont understand how i can change this. thanks for anyhelp

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 18 September 2006 - 07:52 PM

i don't really understand what you're after. you're showing us one row of a table. we don't know what your query is or how you're populating that table at all. mysql_num_rows() simply returns the number of rows that a query has returned. i'm not sure how that's going to help with what you're trying to do. can you be a little more specific? here is an example of a usage of mysql_num_rows():
<?php
$sql = mysql_query("SELECT * FROM tableName");
$count = mysql_num_rows($sql);
echo "There were $count rows returned.";
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 Evanthes

Evanthes
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 18 September 2006 - 08:00 PM

i updated my first post, basically there are multiple records that have the same work_id and i want to display the number of records for each work_id...im gonna keep fiddling with this

#4 craygo

craygo
  • Staff Alumni
  • Advanced Member
  • 1,973 posts
  • LocationRhode Island

Posted 18 September 2006 - 08:03 PM

So you are looking to group the results and then count how many records in that group. correct??? And the grouping is on the work_id??

Ray

#5 Evanthes

Evanthes
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 18 September 2006 - 08:13 PM

yeah exactly, hopefully within the for loop, so it goes in the right spot on my table, and it knows that record we're dealing with

#6 Evanthes

Evanthes
  • Members
  • PipPip
  • Member
  • 25 posts

Posted 18 September 2006 - 08:23 PM

sweet i got it, your statement worked out really well obsidian, thanks a lot!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users