Forum Moderators: coopster

Message Too Old, No Replies

trying to set up a counter

         

disco2424

8:50 pm on Jul 9, 2008 (gmt 0)

10+ Year Member



Hello,

Sorry if this doesn't come across as clear very new to PHP and mysql.
What I'm tring to do is have a display of how many records are added into my database. I have 3 options shipped, returned or instock.
if you search for shipped I want it to display the total number of units that have be shipped at the top of the screen and the same for the other two.

On my seach.html page
shipped - option value = 1
returned -option value = 2
instock - option value = 0

"category" is the field name in the database.

Any help would be greatly appreciated. If you have any suggestion could you please specify where in the script I should place it... sorry like I said very new and trying to learn as much as I can.

This is my results.php page.

<td width="500">Returns = <img src="returns_image.png" border="2" /> Shipped = <img src="shipped_image.png" border="2" /> InStock = <img src="instock_image.png" border="2" />

<table border="1" cellpadding="5" cellspacing="0" bordercolor="#000000">
<tr>
<td width="20"><b>id</b></td>
<td width="150"><b>Name</b></td>
<td width="20"><b>Lane ID</b></td>
<td width="100"><b>Mac address</b></td>
<td width="300"><b>Problem reported</b></td>
<td width="300"><b>Our findings</b></td>
<td width="40"><b>Staff Helping</b></td>
<td width="75"><b>Date</b></td>
<td width="75"><b>Action Taken</b></td>
</tr>
<tr>
<td>

MYSQL_CONNECT($hostname, $username, $password) OR DIE("DB connection unavailable");
@mysql_select_db( "$dbName") or die( "Unable to select database");
?>
<?

//error message (not found message)begins
$XX = "No Record Found, to search again please click either back or search again";
$metode=$_POST["metode"];
$search=$_POST["search"];

//query searches of Returns and shipped
if ($metode=="1" or $metode=="2" or $metode=="0")
{
$search=$metode;
$metode='category';
}

//query details table begins

$query = mysql_query("SELECT * FROM LivePro WHERE $metode LIKE '%$search%'");
while ($row = @mysql_fetch_array($query))
{
$variable1=$row["id"];
$variable2=$row["name"];
$variable3=$row["lane_id"];
$variable4=$row["mac_address"];
$variable5=$row["problem_reported"];
$variable6=$row["our_findings"];
$variable7=$row["staff_helping"];
$variable8=$row["date"];
$variable9=$row["action_taken"];
$variable10=$row["category"];

//shows colors
switch($variable10)
{
case 0:
$variable11='#FFFFFF';
break;
case 1:
$variable11='#AAD0FF';
break;
case 2:
$variable11='#CFCCCC';
break;
}

//table layout for results

print ("<tr bgcolor=$variable11>");
print ("<td>$variable1 &nbsp;</td>");
print ("<td>$variable2 &nbsp;</td>");
print ("<td>$variable3 &nbsp;</td>");
print ("<td>$variable4 &nbsp;</td>");
print ("<td>$variable5 &nbsp;</td>");
print ("<td>$variable6 &nbsp;</td>");
print ("<td>$variable7 &nbsp;</td>");
print ("<td>$variable8 &nbsp;</td>");
print ("<td>$variable9 &nbsp;</td>");
print ("</tr>");
}

//below this is the function for no record!
if (!$variable1)
{
print ("$XX");
}
//end
?>
</table>

eelixduppy

9:38 pm on Jul 9, 2008 (gmt 0)



Hello,

You should run a query like the following to grab all of the counts from your table:


# connect to db server, select db
$types = [url=http://www.php.net/array]array[/url]('in-stock', 'shipped', 'returned');
$query = "SELECT `category`,[url=http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html#function_count]COUNT[/url](*) AS `nums` FROM `LivePro` GROUP BY `category`";
$result = [url=http://www.php.net/mysql-query]mysql_query[/url]($query) or [url=http://www.php.net/die]die[/url]([url=http://www.php.net/mysql-error]mysql_error[/url]());
echo '<table>';
while($row = [url=http://www.php.net/mysql-fetch-assoc]mysql_fetch_assoc[/url]($result)) {
if([url=http://www.php.net/array-key-exists]array_key_exists[/url]($row['category'], $types))
echo '<tr><td>' . $types[$row['category']] . '</td><td>' . $row['nums'] . '</td><tr/>';
}
echo '</table>';

That should definitely get you started :) You might also want to read up here: [dev.mysql.com...]

disco2424

10:39 pm on Jul 9, 2008 (gmt 0)

10+ Year Member



Thanks for the help. I have to run for the day but will let you know if this works.

disco2424

3:44 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



Thank you very much... works great... I will play with it a little to change the layout but is exactly what I wanted it to do.

Thanks again for all your help.

eelixduppy

5:06 pm on Jul 11, 2008 (gmt 0)



Very cool. Glad to help :)

disco2424

5:27 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



Sorry to keep bugging you but I do have another question.
along the same lines as the last one. I would like to give a total when I search for location names. the way it displays now is it shows all units shipped and received under the (name). I was wondering if there was a way to do a query that subtracts the number of returned form the number of shipped to give me the total that would be remaining at that location.

Thanks again for your replies it is such a great help.

eelixduppy

7:24 pm on Jul 11, 2008 (gmt 0)



So given the example I gave above, you just want to subtract two values that it returns? Or are you talking about a new query entirely that groups by Location?

disco2424

7:42 pm on Jul 11, 2008 (gmt 0)

10+ Year Member



Yes I believe it would be a new query that groups by location (name).
On my search page you can search Name, Mac Address, Date, Returned, Shipped and In-stock. With the code you supplied earlier it displays the number of shipped, returned and in-stock. Which is great. I would also like to show the exact number of items for each name. What I mean in when you search by name it will show how many items have been shipped and returned. I would like it to subtract the number returned so I know how many items that location has currently.
ex: shipped 15 items but have returned 5 total that shows should be 10.

I hope I've explained it clearly enough.... I thing I started confusing myself :)

disco2424

6:03 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



Hello,

I managed to get my results page to show the number of shipped and returned for the perticular name I was searching. I just made a few changes to the code you showed me earlier. This is what I've done.

//shows totals when searched by namae
if ($metode =="name")
{
$types = array('In-stock=', 'Returned=', 'Shipped=',);
$query = "SELECT `category`,COUNT(*) AS `nums` FROM `LivePro` WHERE $metode LIKE '%$search%' GROUP BY `category`";
$result = mysql_query($query) or die (mysql_error());
}
echo '<table>';
while($row = mysql_fetch_assoc($result))
{
if(array_key_exists($row['category'], $types))
echo '<tr><td>' . $types[$row['category']] . '</td><td>' . $row['nums'] . '</td><tr/>';
}
echo '</table>';

What happens now is it is displayed like this:

Returned= 1
Shipped= 10
In-stock= 49
Returned= 7
Shipped= 211

What I would like it to do is Subtract the Returned from the Shipped and give me the total.

I would like it to look like this:

Location total=9 (total for that location)

In-stock= 49 (but still give the totals for all machines)
Returned= 7
Shipped= 211

disco2424

6:24 pm on Jul 16, 2008 (gmt 0)

10+ Year Member



Sorry if anybody was responding I managed to make it work.

//shows totals when searched by namae
if ($metode =="name")
{
$types = array('In-stock=', 'Returned=', 'Shipped=',);
$query = "SELECT `category`,COUNT(*) AS `nums` FROM `LivePro` WHERE $metode LIKE '%$search%' GROUP BY `category`";
$result = mysql_query($query) or die (mysql_error());
}
echo '<table>';
while($row = mysql_fetch_assoc($result))
{
if(array_key_exists($row['category'], $types))
$total[$row['category']] = $row['nums'];
}
$there = $total[2] - $total[1];
echo '<tr><td>On-Site=</td><td>'.$there.'</td></tr>';
echo '</table>';

Would still like to thank you for your help the other day.