There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot browser bsod computer crash css dell desktop driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware help please hijackthis hjt hjt log install internet internet explorer itunes javascript keyboard laptop log malware monitor network networking openoffice outlook outlook 2003 outlook express password popups problem router seo slow sound sp3 spyware startup trojan usb video virtumonde virus vista vundo windows windows xp winxp wireless youtube
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
adding a array


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
vipernet's Avatar
Senior Member with 107 posts.
 
Join Date: Aug 2007
02-May-2008, 10:59 AM #1
adding a array
Good day all,

Trying to complete this script and having little issue:

Im running a Count query from multiple tables within SQL and want to add the values and Echo the added value.
This is what I got so far:

PHP Code:
<table border="1" align="center">
<tr>
<?php
$link 
mysql_connect("localhost""root""PASSWORD");
mysql_select_db("dt08_vnc"$link);

$result="SELECT COUNT(*) as value1 FROM bayside UNION All SELECT COUNT(*) as value2 FROM mgteed  WHERE item like '%desktop%'";


$r=mysql_query($result);


while(
$row=mysql_fetch_assoc($r))
//$add=($row['value1']+['value2']);
{

echo 
array_sum ($row);     
//  echo '<tr><td width="50%">'.array_sum['value1','value2'].'Desktops</td></tr>';



     
?>

</tr>
</table>
The output is showing up as:
5716
I want the 2 values to add:
should be 73 (57+16)

The // codes are just things i was playing with,

Any help on what array to use other then array_sum or direction would be great.
Mudley's Avatar
Computer Specs
Member with 99 posts.
 
Join Date: Apr 2008
Experience: Advanced
02-May-2008, 11:26 AM #2
//i'm not sure what your query is returning (one row, or multiples) - so try this

$sum = 0;
while($row=mysql_fetch_assoc($r))
{
$sum = $row['value1'] + $row['value2];
echo $sum;
// echo '<tr><td width="50%">'.array_sum['value1','value2'].'Desktops</td></tr>';
$sum = 0; //reset
}
vipernet's Avatar
Senior Member with 107 posts.
 
Join Date: Aug 2007
02-May-2008, 11:39 AM #3
Thanks allot for your reply.

I tried your code:

PHP Code:
$result="SELECT COUNT(*) as value1 FROM bayside UNION All SELECT COUNT(*) as value2 FROM mgteed  WHERE item like '%desktop%'";


$r=mysql_query($result);

$sum 0;
while(
$row=mysql_fetch_assoc($r))
{
$sum $row['value1'] + $row['value2'];
echo 
$sum;


$sum 0//reset

     
?> 
Output reads:
Quote:
Notice: Undefined index: value2 in E:\WWW\reports\summary.php on line 18
57
Notice: Undefined index: value2 in E:\WWW\reports\summary.php on line 18
16
I think my issue is with the SQL query. the second
PHP Code:
COUNT 
Quote:
value2
isn't given a value. So the count is actually displaying the numbers 57 and 16 as the result of
Quote:
Value1
Mudley's Avatar
Computer Specs
Member with 99 posts.
 
Join Date: Apr 2008
Experience: Advanced
02-May-2008, 11:53 AM #4
so its returning 2 rows, but only value1 for each row, no value2s?

lets find out what your query is returning. (i've never used UNION before)
<?
$result="SELECT COUNT(*) as value1 FROM bayside UNION All SELECT COUNT(*) as value2 FROM mgteed WHERE item like '%desktop%' LIMIT 1";


$r=mysql_query($result);

row=mysql_fetch_assoc($r);
echo "<xmp>";
print_r ($r);
echo "</xmp>";
?>
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 03:43 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.