PHP - Adding
This might be more of a mySQL question or a PHP question, I'm not exactly sure. I have a field in my table (Deals) called "Price". All I want is the sum of that field. I tried using something like: $price += $row['Price']; This ended up as one long string, even though the database is set as a decimal number field. Then I tried doing it from mySQL like this (from what I read), but I don't think I have the full answer: foreach($stmt->query('SELECT SUM(Price) FROM Deals') as $row) { echo "<tr>"; echo "<td>Total</td>"; echo "<td> ->".$row['SUM(Price)']."</td>"; echo "</tr>"; } Anyway, if someone could just show me how to get that sum, I would greatly appreciate it because I am missing something here. Similar TutorialsHi can someone please show me how I should include a Code: [Select] <div id="css"></div> in this line Code: [Select] echo $AB_LANGS['CalendarInstructions']; I have tried several attempts with no joy. Many thanks hi i been working on this code witch will add what ever text i want to my database all is working great but what i am having problems with is inserting a url in the same field as vidID it will look like this in the database hxxp://xxx.mysitehere.com/index.php?vid=vidID hope you guys know what im talking about im having a hard time explaining it sorry. Code: [Select] <?php $host="xxxxx"; $username="xxxxxxx"; $password="xxxxxxx"; $db_name="xxxxxxx"; $tbl_name="xxxxxxx"; $message = strtoupper(trim($_REQUEST['message'])); $sender = strtoupper(trim($_REQUEST['sender'])); $email = strtolower(trim($_REQUEST['email'])); $vidID =$_REQUEST['vidID']; mysql_connect("$host", "$username", "$password")or die("cannot connect server "); mysql_select_db("$db_name")or die("cannot select DB"); $sql="INSERT INTO $tbl_name(message,sender,email,vidID)VALUES('$message','$sender', '$email', '$vidID')"; $result=mysql_query($sql); ?> This is my current script <?php $iFile = "accounts.txt"; //Put your list lagger's info in this txt file in the same directory as this script. Format is UserID(space)AuthKey $login = file($iFile, FILE_SKIP_EMPTY_LINES); if(!is_file($iFile)) echo "The logins file couldn't be found...".sleep(999999); foreach($login as $line_num => $line) { $login = explode(" ", htmlspecialchars(str_replace(" "," ",$line))); //////////////// ////////////// /////////// ///////// if(stristr($login[1], "\n")) $login[1] = substr($login[1], 0, strlen($login[1])-2); $MobLink = "http://mobsters-fb-apache-dynamic-lb.playdom.com/mob_fb/"; $RefreshStat = file_get_contents($MobLink."refresh_stat?user_id=".$login[1]."&auth_key=".$login[0]); $Mob_Name = explode("<mob_name>", $RefreshStat); $Mob_Name = explode("<", $Mob_Name[1]); $Name = $Mob_Name[0]; $Cash = explode("<cash>", $RefreshStat); $Cash = explode("<", $Cash[1]); $CurrentCash = $Cash[0]; echo $Name."-$".number_format($CurrentCash)." - ".$login[1]."\n"; } sleep(99999); ?> The Script runs through for 100 different accounts, is it possible to add all the Cash values that get echo'd? Hello, I'm just learning PHP and I'm trying to have a different image display for each Item in a shop.. My only guess to how this would be possible is to name the IMG after the ID of the Item, like Item #10 has a name of 10.jpg. My question is, how would I go about doing this? or is there a better way of doing this.. Here is the Code I currently have. Thanks for help. Code: [Select] <?php session_start(); $page = 'index.php'; mysql_connect ('localhost','root','') or die(mysql_error()); mysql_select_db ('buggs') or die (mysql_error()); function products() { $get = mysql_query('SELECT id, name, description, price FROM products WHERE quantity > 0 ORDER BY id DESC LIMIT 10'); if (mysql_num_rows($get)==0) { echo "There are no products to display!"; } else { while ($get_row = mysql_fetch_assoc($get)) { echo '<p>'.$get_row['id'].'<br />'.$get_row['name'].'<br />'.$get_row['description'].'<br />'.number_format($get_row['price'], 2).' <a href="cart.php?add='.$get_row['id'].'">Add</a></p>'; } } } ?> and the code to where it displays.. Code: [Select] <?php require 'cart.php'; ?> <html> <head> </head> <body> <?php products();?> </body> </html> I have seen somewhere that you can add to a string like so $var = "text"; or $string ="var"; &$var = "text"; it is the latter that I am wanting to know more about. Is it possible to add onto the value of a $_SESSION, rather replacing it? $_SESSION['order']['cartcontenttext'] += echo 'Small: "'.$content['sizes'].'"'; $_SESSION['order']['cartcontenttext'] += echo 'Medium: "'.$content['sizem'].'"'; $_SESSION['order']['cartcontenttext'] += echo 'Large: "'.$content['sizel'].'"'; ... so the result would be something like this: echo $_SESSION['order']['cartcontenttext']; Quote Small: 1Medium: 3Large: 2 Hi, This is probably very easy for someone who knows how but I cant seem to get it working. Can someone please tell me how I can alter this query to select a 'WHERE gender = female' so instead of picking a random user it picks a random female user ? Code: [Select] $offset_result = mysql_query( " SELECT FLOOR(RAND() * COUNT(*)) AS `offset` FROM `users` "); $offset_row = mysql_fetch_object( $offset_result ); $offset = $offset_row->offset; $user1b = mysql_query( " SELECT * FROM `users` LIMIT $offset, 1 " ); $user1= mysql_fetch_array($user1b); Also, would it be easy to modify it so it picks a second random user that isnt the same as the first user ? so I end up with 2 random females ( $user1 and $user2 ) Many thanks, Scott nevermind. hello, i have created a table that desplays perticulars, price per person and number of people. the fees is then calculated by multiplying price per person with number of people. till now everything is good. but the problem is when i try to calculate the total fees. here is an example to explain you better: Perticulars Price Per Person Number of people Amount something 10 100 1000 something else 20 100 2000 Total 3000 Code: [Select] $pert_query = mysql_query ("SELECT * FROM `perticulars` WHERE `invoice` =$invoice") or die(mysql_error()); $i = 1; $sum = 0 + $_SESSION['sum']; while($perticulars = mysql_fetch_array($pert_query, MYSQL_ASSOC)) { ?> <tr> <td style="border-collapse: collapse"><div align="center"><?php echo $i; $i++; ?></div></td> <td style="border-collapse: collapse"><div align="center"><?php echo $perticulars['perticulars']; ?></div></td> <td style="border-collapse: collapse"><div align="center"><?php echo $perticulars['ppc']; ?></div></td> <td style="border-collapse: collapse"><div align="center"><?php echo $perticulars['nop']; ?></div></td> <td style="border-collapse: collapse"><div align="center"> <?php $amount = $perticulars['ppc'] * $perticulars['nop']; echo $amount; $_SESSION['sum'] = $sum+$amount;?> </div></td> </tr><?php } ?> <tr> <td colspan="4" style="border-collapse: collapse"><div align="center">Total</div></td> <td style="border-collapse: collapse"><div align="center"><?php echo $sum; ?></div></td> </tr> </table> From the above code you can see that i am getting the amount form $amount = $perticulars['ppc'] * $perticulars['nop']; by this each row has its own amount which i got by multiplying the data from mysql. the problem is how do i find out the Total. i know i am suppose to add up $amount but how? i even tired using sessions to store the amount and add the new amount to get total but the problem in using sessions is it messes up my next page, no using session is a very bad idea. i need help with adding an integer into an array something like this $Loaded[count($Loaded)+1] = $To; Hi again guys, Right, I have two times being output from a database... The first time is the time something is booked in for (eg: 16:30:00) and the second time is the duration that thing is booked in for (eg an hour and a half - 01:30:00) Now, I want to figure out when that thing will be finished, so using the two examples above, it will be finished at 18:00:00, I've tried a few things with no success Please help Cheers I created the code below and it outputs the following number output: 1111111773526604115551144611337112281111221111345 6789101121111100352004110051100611007110081100221 1003456789101141111100352004110051100611007110081 1002211003456789101151111100352004110051100611007 1100811002211003456789101123111110035200411005110 06110071100811002211003456789101134 Anyways if you look at the code, I need to be able to add the values on a single colony, idcol, with the same type and subtype, btid/stid, generated as the $prod variable. buildings with the same subtype generate the same resource, in this case, several iron mines and a single copper mine are present, but there are also 9 other types of resource buildings. How do I collect on the prods which go together? Code: [Select] <?php $dbhost = 'localhost:3306'; $dbuser = 'lordofth_aos'; $dbpass = 'flarge'; $dbname = 'lordofth_aos'; $conn = mysql_connect($dbhost,$dbuser,$dbpass) or die ('Error connecting to mysql'); mysql_select_db($dbname); $query="SELECT id FROM users"; $result=mysql_query($query); $num=mysql_num_rows($result); $i=0; $idn=1; while($i<$num){ $queryii="SELECT idcol FROM colony WHERE id=$idn"; $resultii=mysql_query($queryii); $numt=mysql_num_rows($resultii); $id=mysql_result($result, $i); echo $id; $ii=0; $ibt=1; while($ii<$numt){ $querypty="SELECT btid FROM ptypes WHERE btid=$ibt"; $resultpty=mysql_query($querypty); $numpty=mysql_num_rows($resultpty); $idcol=mysql_result($resultii, $ii); echo $idcol; $bt=0; $btid=mysql_result($resultpty, $bt); echo $btid; $bte=1; while($bt<$numpty){ $querystid="SELECT stid FROM ptypes WHERE stid=$bte"; $resultstid=mysql_query($querystid); $numstid=mysql_num_rows($resultstid); $st=0; $stid=mysql_result($resultstid, $st); echo $stid; $ste=1; while($st<$numstid){ $bidum=0; $querybpid="SELECT bpid FROM blueprints WHERE btid=$btid AND stid=$stid"; $resultbpid=mysql_query($querybpid); $numbpid=mysql_num_rows($resultbpid); while($bidum<$numbpid){ $bidumb=0; $bidumbg=mysql_result($resultbpid, $bidum, 'bpid'); echo $bidumbg; $querybid="SELECT COUNT(bid) AS count, workers, efficiency FROM buildings INNER JOIN blueprints ON buildings.bpid=blueprints.bpid WHERE blueprints.btid=$btid AND blueprints.stid=$stid AND buildings.bpid=$bidumbg AND buildings.idcol=$idcol"; $resultbid=mysql_query($querybid); $staff=mysql_result($resultbid, $bidumb, 'workers'); $effic=mysql_result($resultbid, $bidumb, 'efficiency'); $count=mysql_result($resultbid, $bidumb, 'count'); echo $staff; echo $effic; echo $count; $prod=$staff*$effic*$count; echo $prod; $bidumb++; $bidum++; } $st++; $ste++; } $bt++; $bte++; } $ii++; } $i++; $idn++; } ?> I'm trying to get code to work along the following logic (or something similar if this logic isn't PHP-friendly). If a user inputs a URL with the http, I want the url left alone. If the user inputs the URL starting with www, I want http:// added to the beginning. If the user does anything else (in other words uses a relative link), I want the beginning of my url to be added on (because then I later check for it to open the website in a new window or not). Code: [Select] if (strpos($url, "http") == 0) { $url = $url; } else if (strpos($url, "www") == 0) { $url = "http://".$url; } else { $url = "http://www.mywebsite.com/".$url; }; Any suggestions? hello. im trying to get the following to work. any ideas for me? Code: [Select] Text[12]=["Job Description","$description it "] it is in a .js file. Hi, I have a function that pulls a random line from a text file and I would like to change it so it pulls a random line from a text file that contains a certain date, heres the current function : Code: [Select] function RandomLine($filename) { $lines = file($filename) ; return $lines[array_rand($lines)] ; } Any ideas on the best way to do this ? Many thanks. I am trying to add foo onto bar inside a loop so I can then echo bar outside the loop and see the sum. The PHP manual doesn't have anything on addition in the way I need it (adding onto), so I was wondering if anyone can help me? LOL embrace yourself! $query = "select * from products where id='{$content['id']}'"; $result = mysql_query($query); $row = mysql_fetch_array($result); mysql_query ($query); mysql_close($link); echo " <B>Product ID:</B> {$content['id']} <BR> <B>Price:</B> $row[price] AUD <BR> <BR> "; +$row[price] -> $total; } echo $total; what is the propper way to write Code: [Select] $payrollweek="10"; date('Y-m-d', strtotime("+($payrollweek - 2) week $payrollend") ), "<br />"; its the ("+($payrollweek - 2) that isnt working i think I had this working before, but my coding structure has changed a bit and need help with syntax. My function I have down ok. function cssfrommvp($mvp) { $class = array('MVP' => 'MVP'); return $class[$mvp]; } this is how I would add the class to a <td> with $row. Code: [Select] echo "<td class=\"".cssfrommvp($row['mvp'])."\">".$row['mvp']."</td>\n"; now using $array <td> . $MVP .</td>\n</tr> how would I do the same thing as done before with $row? hi all I have this can someone help me I'm trying to get a total cost <?php //inventory report printable total value in stock $connect = mysql_connect('*******') or die (mysql_error()); mysql_select_db('fminventory') or die (mysql_error()); $query = mysql_query("SELECT SUM(cost) FROM cars"); $query2 = mysql_fetch_array($query); echo $query2; ?> in my php mail i use this code: $success = mail($EmailFrom, $Subject, $Body2, "From: <$EmailTo>"); and here are the variables for emial from and email to, $EmailFrom = Trim(stripslashes($_POST['email'])); $EmailTo = "info@mydomain.co"; when the email is received the sender just appears as info@mydomain.co how can i have it say MSUKGroup Auto Responder or something? instead of just repeat the address? |