Jump to content

phpstuck

Members
  • Posts

    59
  • Joined

  • Last visited

Profile Information

  • Gender
    Not Telling

phpstuck's Achievements

Member

Member (2/5)

0

Reputation

  1. Here are the errors: Notice: Undefined variable: results in C:\xampp\htdocs\report1.php on line 18 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\report1.php on line Here is the db.php stuff <?php $dbc = new mysqli("127.0.0.1", "root", "abc1234", "supplies"); if ($dbc->connect_errno) { echo "Failed to connect to MySQL: (" . $dbc->connect_errno . ") " . $dbc->connect_error; } //if (mysqli_ping($dbc)) //{echo 'MySQL Server ' .mysqli_get_server_info($dbc) . // ' on ' . mysqli_get_host_info($dbc); // //} ?> I tested the connection with a ping and it returned the right printed info... Just getting the mysqli changed seems to be where I get hung up. Thanks again.
  2. I have read and re-read everything I can find. I could re-write my whole site but that doesn't seem practical. I am trying to get this old piece of code to work within the new standard and just can't quite grasp it I guess... I am an amateur at this I know, but any help would be appreciated... Thanks <?PHP include_once 'db.php'; include_once 'header.html'; $sql = mysqli_query($dbc, "ALTER TABLE inven ORDER BY cat ASC, descrip ASC, brand ASC"); echo "<br><hr>"; $deflist=mysqli_query($dbc, "SELECT quant, brand, descrip, size, flavor, cat FROM inven"); while ($all = mysqli_fetch_array($deflist)) { $results[$all['cat']][] = array ('quant' => $all['quant'], 'brand' => $all['brand'], 'descrip' => $all['descrip'], 'size' => $all['size'], 'flavor' => $all['flavor']); } //print_r($results); foreach ($results as $catName => $catData) { print('<b><br><font face=tahoma size=4>'.$catName.'</b><br/></font>'."\n"); foreach ($catData as $itemNum => $itemData) { // if you want to access the row data in this loop, use the following method: print($itemData['descrip'].', '.$itemData['brand'].', '.$itemData['size'].', '.$itemData['flavor'].' - '.$itemData['quant'].'<br/>'."\n"); // etc. (you must code the field names in hard coded this way) //foreach ($itemData as $fieldName => $value) } } include_once 'footer.html'; ?>
  3. If I do that the only thing that prints to the browser is: (and no errors on the mysql_error() ) Baking Soda On Hand: , Minimum: 4 To Buy: 2 It echos those two lines and nothing else follows
  4. I solved a bigger problem and thought I was on the way to finishing this particular page. I am pulling from two tables. I am trying to SUM() the quantity from the group. If I pull from only one table it works like a dream... <?php include_once 'db.php'; $query = "SELECT groccat, SUM(quant) FROM inven GROUP BY groccat"; $result = mysql_query($query) or die(mysql_error()); // Print out result while($row = mysql_fetch_array($result)){ echo "Total ". $row['groccat']. " = ". $row['SUM(quant)']; echo "<br />"; } ?> That works like a charm to get a total of all similar items. However when pulling from two tables I get this errror: Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in D:\xampp\htdocs\inventory\shoppinglist.php on line 10 Warning: Invalid argument supplied for foreach() in D:\xampp\htdocs\inventory\shoppinglist.php on line 17 Here is the code I am trying to get the sum to work in. <?php include_once 'db.php'; echo "<hr>"; $deflist=mysql_query( "SELECT l.groccat, l.SUM(quant), b.grocname, b.min, b.tobuy FROM inven l, groc b WHERE l.groccat = b.grocname GROUP BY grocname"); while ($all = mysql_fetch_array($deflist)) { $results[$all['grocname']][] = array ('quant' => $all['SUM(quant)'], 'min' => $all['min'], 'tobuy' => $all['tobuy']); } foreach ($results as $catName => $catData) { print('<center><TABLE id=AutoNumber20 style="BORDER-COLLAPSE: collapse" borderColor=#000000 bgcolor=blue height=12 cellSpacing=3 cellPadding=3 width=600 border=1> <TBODY> <TR><TD> <b><font face=arial size=2 color=white>'.stripslashes($catName).'</b><br/></font></td></tr></table></center>'."\n"); foreach ($catData as $itemNum => $itemData) { print('<center><TABLE id=AutoNumber21 style="BORDER-COLLAPSE: collapse" borderColor=#000000 height=12 cellSpacing=3 cellPadding=3 width=600 border=0> <TBODY> <TR><TD><font face=arial size=2>On Hand: ' .$itemData['quant'].',<b> Minimum: '.$itemData['min'].'</b> To Buy: '. $itemData ['tobuy']. '<br/></td></tr></table></center></font>'."\n"); } } echo '</td></tr></table></center>'; ?> If I take out the two references to SUM() it prints the following results in the browser. I want to total all ON HAND things under the single group heading. Instead of it printing a new line for each brand under the heading. Baking Soda On Hand: 1, Minimum: 4 To Buy: 2 Mollasses Standard Size On Hand: 2, Minimum: 3 To Buy: 2 Cake Mix On Hand: 2, Minimum: 12 To Buy: 3 On Hand: 2, Minimum: 12 To Buy: 3 On Hand: 1, Minimum: 12 To Buy: 3 Muffin Mix On Hand: 1, Minimum: 4 To Buy: 4 On Hand: 3, Minimum: 4 To Buy: 4 Canned Corn 10 - 12 oz. On Hand: 10, Minimum: 36 To Buy: 10 Mens Deodorant On Hand: 2, Minimum: 12 To Buy: 5 On Hand: 1, Minimum: 12 To Buy: 5 On Hand: 1, Minimum: 12 To Buy: 5 On Hand: 1, Minimum: 12 To Buy: 5 On Hand: 1, Minimum: 12 To Buy: 5 On Hand: 0, Minimum: 12 To Buy: 5 See if there is more than one brand it shows a total for each brand, whereas I want it to just add up the on hands for each category and show a single total on hand. Like this: Baking Soda On Hand: 1, Minimum: 4 To Buy: 2 Mollasses Standard Size On Hand: 2, Minimum: 3 To Buy: 2 Cake Mix On Hand: 5, Minimum: 12 To Buy: 3 ANY HELP WOULD BE GREATLY APPRECIATED :-)
  5. Ok I have them all printing to the screen, I just have to work out the math now.
  6. Thanks for asking me to look at that again... I realize where that is... But if I move anything at all it won't return anything to the screen. I'm here asking for help after researching this for several days. This is my first attempt at trying to return these results and adding an IF statement, I have been all over the PHP manual and searching the internet high and low and coming up empty. Maybe I'm using the wrong kind of query and I'll never get to point B... I don't know. I used this same query and produce results for other types of problems within my application, but this one has the MATH and IF involved. Maybe I'll try explaining a little better what I'm trying to do and you'll be able to point me to a place where I can get a little help constructing it. I'm not asking you to write it, but maybe a little more in the line of a complete answer and I'd be on my way to learning something. I have inventory of groceries and supplies in the first table. People use a barcode scanner to keep track of thing that are on hand, when they take one out of inventory they scan it and it is removed from the database, one field is GROCCAT thats where they link all the different brands of a similar product (say for instance canned corn 10 oz.) the GROCCAT will be "canned corn" for all brands. The second table is where all the GROCCATS are created and stored under the name GROCNAME, in the second table along with GROCNAME there is a field called MIN where the user decides how many canned corn should be on hand as a minimum (for example 36) when the minumum is reached in the main inventory a trigger to buy is set, there is one more field called TOBUY, when the minimum is reached the TOBUY field says how many to buy at that point. So I am trying to build a query where it pulls from both tables and matches up items based on GROCCAT = GROCNAME then it adds together all the different brands as a total and checks to see if they are below the set minimum... Thus the IF statement and if they are below minimum a echo is sent to buy canned corn add together the min + tobuy and print that to the screen. Somewhere along the way I think I am trying to use the wrong query or results return to make this work... Please if you have any constructive ideas let me know where to look for help answering this question.
  7. Just under }ELSE{ is the $itemData reference Which brings up another interesting point... I don't really want the ELSE statement, but it fails completely without it. arrays are somewhat new to me when calculating from them. I have researched the world over and can't seem to find anything that applies to this problem. Sorry about the indents, this code has been worked and reworked so many times just trying to get it to work, I'm half way there, at least it prints from the database now in relation to the items under min. Thanks for the heads up on the <?php highlighting. <?php include_once 'db.php'; echo "<hr>"; $deflist=mysql_query( "SELECT l.groccat, l.quant, b.grocname, b.min, b.tobuy FROM inven l,groc b WHERE l.groccat = b.grocname"); while ($all = mysql_fetch_array($deflist)) { $results[$all['grocname']][] = array ('quant' => $all['quant'], 'min' => $all['min'], 'groccat' => $all['groccat'], 'tobuy' => $all['tobuy']); } foreach ($results as $groName => $groData) IF ($groName['quant'] < $groName['min']){ Echo "You need " . (($itemData['tobuy'] + $itemData['min']) - ($itemData['quant'])) ." - " . $groName ."<br>"; { foreach ($groData as $itemNum => $itemData) //Echo "You need " . (($itemData['tobuy'] + $itemData['min']) - ($itemData['quant'])) ." - " . $groName ."<br>"; { } } } ?>
  8. I am trying to do a calculation within an array. I finally have the array printing the words I need but the math will just not work out... I either get all zeros for amount needed or I get all 5's for amount needed... I need the right answer though...LOL I am pulling data from two tables. It prints a list of items that are below the set minimums. The quantity on hand is on one table and the mimimum allowed is in another table along with another field that instructs how many over min. to buy when the min. is reached. Simple math would say $need = (($min + $tobuy) - ($quant)); However this is in an array and I am missing something, somewhere.... My Code as follows: <?php include_once 'db.php'; echo "<hr>"; $deflist=mysql_query( "SELECT l.groccat, l.quant, b.grocname, b.min, b.tobuy FROM inven l,groc b WHERE l.groccat = b.grocname"); while ($all = mysql_fetch_array($deflist)) { $results[$all['grocname']][] = array ('quant' => $all['quant'], 'min' => $all['min'], 'groccat' => $all['groccat'], 'tobuy' => $all['tobuy']); } foreach ($results as $groName => $groData) IF ($itemData['quant'] < $itemData['min']){ $need = (($itemData['tobuy'] + $itemData['min']) - $itemData['quant']); Echo "You need " . $need ." - " . $groName ."<br>"; }ELSE{ { foreach ($groData as $itemNum => $itemData) { } } } ?>
  9. Ok I figured this part out.... Now the math is killing me :-(
  10. OK can someone please point me in the right direction... I have been through the manual, and all over google but I can't seem to figure this out. I have two tables, one is named inven the other is named groc In the inventory table there are food items made by different manufactures like Kroger canned corn, Aldi Canned Corn and Foodland Canned corn. each have a different "quan" They are linked by a sub cat in the "inven" table named groccat (all are set to "canned corn") Over in the "groc" table there is a grocname that is also "canned corn" and a column named "min" I am trying to get a sum from an array for all the different brands in the "inven" table that have a sub cat of "canned corn" So instead of 2 cans of kroger and 3 cans of Aldi... I can get 5 cans of corn. I have tried the following to no avail. along with a million other ways to try and put it together... Any help would be enough to get me moving again :-) <?PHP include_once 'db.php'; $deflist=mysql_query( "SELECT l.groccat, l.quant, b.grocname, b.min, b.tobuy FROM inven l,groc b WHERE l.groccat = b.grocname"); while ($all = mysql_fetch_array($deflist)) { $results[$all['groccat']][] = array ('quant' => $all['quant'], 'grocname' => $all['grocname'], 'min' => $all['min'], 'tobuy' => $all['buy']); } $newquant = SUM($all[;quant']); echo $newquant; ?>
  11. Actually I figured it out using a simple PHP / MySQL test! <?PHP include_once 'db.php'; $sql=mysql_query( "SELECT groccat FROM book"); if (!$sql){ mysql_query("ALTER TABLE book ADD groccat VARCHAR(60) NOT NULL AFTER notes"); echo 'groccat created'; }ELSE{ //from here just continue the page as usual! echo 'groccat already exists!'; } ?>
  12. Because it keeps people from having to add them manually. This application runs on computers not on the Internet. People serve it from their own computers running a PWS (xampp -Apache, MySQL - PHP) When I create updates it is easy to create a new table IF NOT EXISTS for new sections, on rare occasions I might need to add a column to a table that already exists.
  13. I am trying to figure out if I can ALTER a table and add a COLUMN where the condition is IF NOT EXISTS... I can create a whole new table that way. But can I ALTER an already existing table and add the new column's if they don't already exist. If they already exist then just skip it and move on. This is for an update I am providing to quite a few people who are already running this application on their own computers using a localhost address. Here is what I use for creating the table IF NOT EXISTS $query = "CREATE TABLE IF NOT EXISTS `book` ( `id` int(100) NOT NULL AUTO_INCREMENT, `upc` varchar(100) NOT NULL, `title` varchar(200) NOT NULL, `author` varchar(100) NOT NULL, `publisher` varchar(100) NOT NULL, `pages` varchar(100) NOT NULL, `genre` varchar(200) NOT NULL, `rating` varchar(100) NOT NULL, `notes` longtext NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;"; Say I want to add a column named "grouping" (`grouping` varchar(200) NOT NULL,) to a table that already exists... Is this possible. I don't want to generate an error and have to handle it later with more code.
  14. The code below returns results when a person loans a book to someone. It provides an array result that shows which person has which books currently signed out.What I am trying to do is bring info from another table into the results. The primary table this works off of returns the name of the borrower and the UPC number of the book they have borrowed as well as the date it was checked out. I have another table named BOOK where the same UPC code is located but it has the title of the book associated with it. In the table named LOANBOOK the UPC is called "loanupc" In the table named BOOK the UPC is called just "upc" Can I run a query with INNER JOIN that would put the title of the book in the returned results based on matching those UPC codes. Here is the code for what I have which returns only info from the LOANBOOK table. <?PHP include_once 'db.php'; include_once 'header.html'; include_once 'reportheader.html'; $sql = mysql_query( "ALTER TABLE loanbook ORDER BY name ASC"); echo "<hr>"; $deflist=mysql_query( "SELECT loanupc, name, dateout, datein FROM loanbook WHERE datein IS NULL"); while ($all = mysql_fetch_array($deflist)) { $results[$all['name']][] = array ('loanupc' => $all['loanupc'], 'dateout' => $all['dateout'] ); } //print_r($results); error_reporting(0); echo '<center><TABLE id=AutoNumber22 style="BORDER-COLLAPSE: collapse" borderColor=#000000 height=12 cellSpacing=3 cellPadding=3 width=600 border=1> <TBODY> <TR><TD>Books Currently loaned out</TR></TD><TR><TD>'; foreach ($results as $catName => $catData) { print('<center><TABLE id=AutoNumber20 style="BORDER-COLLAPSE: collapse" borderColor=#000000 bgcolor=green height=12 cellSpacing=3 cellPadding=3 width=600 border=1> <TBODY> <TR><TD> <b><font face=arial size=2 color=white>'.$catName.'</b><br/></font></td></tr></table></center>'."\n"); foreach ($catData as $itemNum => $itemData) { error_reporting(1); // if you want to access the row data in this loop, use the following method: print('<center><TABLE id=AutoNumber21 style="BORDER-COLLAPSE: collapse" borderColor=#000000 height=12 cellSpacing=3 cellPadding=3 width=600 border=0> <TBODY> <TR><TD><font face=arial size=2> <A HREF="editproduct.php?upc='.$itemData['loanupc'].'">edit</A> - ' . $itemData['loanupc'].', '.$itemData['dateout'].'<br/></td></tr></table></center></font>'."\n"); // etc. (you must code the field names in hard coded this way) //foreach ($itemData as $fieldName => $value) } } echo '</td></tr></table></center>'; include_once 'footer.html'; ?>
  15. { move_uploaded_file($temp, "uploads/".$name); echo "Upload complete! Please use your browser to navigate back"; include_once 'nameof page.php'; } } ?>
×
×
  • 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.