elindithas Posted July 30, 2009 Share Posted July 30, 2009 Hi, just getting started here, got to where i can connect, setup database, call info etc what im trying to do is call something from my database "mtgshack" the table is "m10" by date "Added" (is the id field 2000/10/10 or whatever) I want to call everything that was added to the database within the last like month, so as to be able to display something like "New Product" as its a card singles website any one able to help? I think i know what to do kinda, im just not used to the system yet Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/ Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 Try this: <?php $date = date("Y-m-d",mktime(0,0,0,date('m') - 1,date('d'),date('Y'))); $query = "SELECT * FROM `m10` WHERE `Added` >= '$date'"; $run = mysql_query($query); if($run){ $results = array(); while($arr = mysql_fetch_assoc($run)){ $results [] = $arr; } print_r($results); }else{ die("Unable to connect to database."); } ?> Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886476 Share on other sites More sharing options...
elindithas Posted July 30, 2009 Author Share Posted July 30, 2009 wow, worked! thanks it is however printing results like this Array ( [0] => Array ( [iD] => 1 [Name] => Garruk Wildspeaker => Green [Type] => Planeswalker [Rarity] => Mythic [Price] => 23 [stock] => 000 [Added] => 2009-07-22 ) [1] => Array ( [iD] => 2 [Name] => Ajani Goldmane => White [Type] => Planeswalker [Rarity] => Mythic [Price] => 20 [stock] => 003 [Added] => 2009-07-22 ) ) anyway to clean it up as it displays? Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886480 Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 You can make it display however you like, the print_r($array) simply displays the results to test if it worked. How did you want it to look? Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886488 Share on other sites More sharing options...
vineld Posted July 30, 2009 Share Posted July 30, 2009 print_r is simply a quick way (usually for testing and debugging purposes) to print the content of an array. If you want to display the content in any normal fashion you will have to loop through the array. Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886492 Share on other sites More sharing options...
elindithas Posted July 30, 2009 Author Share Posted July 30, 2009 id like it to display in a simple table, would i just put my information where the print array is? Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886537 Share on other sites More sharing options...
pengu Posted July 30, 2009 Share Posted July 30, 2009 <?php $date = date("Y-m-d",mktime(0,0,0,date('m') - 1,date('d'),date('Y'))); $sql = "SELECT * FROM `m10` WHERE `Added` >= '$date'"; $result = mysql_query($sql); while($rows = mysql_fetch_assoc($result)) { echo "<tr><td>" .$row['somename']. "</td></tr>"; } ?> that kind of thing Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886540 Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 <?php $date = date("Y-m-d",mktime(0,0,0,date('m') - 1,date('d'),date('Y'))); $sql = "SELECT * FROM `m10` WHERE `Added` >= '$date'"; $result = mysql_query($sql); while($rows = mysql_fetch_assoc($result)) { echo "<tr><td>" .$row['somename']. "</td></tr>"; } ?> that kind of thing assuming of course you have <table></table> tags on the outside of the php logic. Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886546 Share on other sites More sharing options...
elindithas Posted July 30, 2009 Author Share Posted July 30, 2009 hmm, its not resulting in all the names the print_r did, just 1 name repeated for some reason sorry about the hassle im very new at this, but with each step its making more sense Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886548 Share on other sites More sharing options...
pengu Posted July 30, 2009 Share Posted July 30, 2009 assuming of course you have <table></table> tags on the outside of the php logic. yeah, was just an example Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886550 Share on other sites More sharing options...
p2grace Posted July 30, 2009 Share Posted July 30, 2009 This should do the trick <?php $date = date("Y-m-d",mktime(0,0,0,date('m') - 1,date('d'),date('Y'))); $sql = "SELECT * FROM `m10` WHERE `Added` >= '$date'"; $result = mysql_query($sql); while($arr= mysql_fetch_assoc($result)) { echo "<tr><td>" .$arr['somename']. "</td></tr>"; } ?> Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-886572 Share on other sites More sharing options...
elindithas Posted July 31, 2009 Author Share Posted July 31, 2009 worked perfectly, <3 u one more question, i have my database all set up, the fields are name, color, type, rarity, price, stock, and date added im curious as to how i set a script to insert new data into the database (basicly thats no prob) the thing is how do i set it so if you fill in a name and value of something that is already there lets say as an example (magic cards) Lightning Bolt. thats the name and the price etc would all be the same, how do i get it to just add into the previous entry and change the stock value up by 1? otherwise just to post a new card as a completely new entry thanks in advance, you guys have been super helpful! Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-887569 Share on other sites More sharing options...
Adam Posted July 31, 2009 Share Posted July 31, 2009 Look into MySQL UPSERTS. Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-887573 Share on other sites More sharing options...
elindithas Posted July 31, 2009 Author Share Posted July 31, 2009 oohh, i think thats what i needed, I love you now as well. Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-887580 Share on other sites More sharing options...
elindithas Posted July 31, 2009 Author Share Posted July 31, 2009 back again, got everything set (getting to the update over insert a bit later) im wondering what in my code is giving me a Notice: Undefined index: Name in C:\wamp\www\website\dwc.php on line 11 for all my variables, im sure its something stupid <?php require_once("Connections/connection.php"); //database connection /////////////////////////////////// $query = sprintf("SELECT * FROM m10"); $result = @mysql_query($query); $row = mysql_fetch_array($result); /////////////////////////////////// ////////////////////////////////// $Name = $_POST['Name']; $Color = $_POST['Color']; $Type = $_POST['Type']; $Rarity = $_POST['Rarity']; $Price = $_POST['Price']; $Stock = $_POST['Stock']; $Added = $_POST['Added']; $submit = $_POST['submit']; ///////////////////////////////// if ($submit && $Name && $Color && $Rarity && $Price && $Stock && $Added) { $query = sprintf("Insert into m10 (Name, Color, Type, Rarity, Price, Stock, Added) values ('$Name', '$Color', 'Type', 'Rarity', 'Price', 'Stock', 'Added')"); mysql_query($query)or die(mysql_error()); }elseif ($submit){ echo "One of your fields is empty !"; } /*do { echo $row['field3']. "<br>"; }while ($row = mysql_fetch_array($result))*/ ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <p> </p> <form id="form1" name="form1" method="post" action="<?php $_SERVER['PHP_SELF']; ?>"> <table width="400" border="1" cellspacing="0" cellpadding="0"> <tr> <td>Name</td> <td>Color</td> <td>Type</td> <td>Rarity</td> <td>Price</td> <td>Stock</td> <td>Added</td> </tr> <tr> <td><label> <input name="Name" type="text" id="Name" /> </label></td> <td><label> <select name="Color" id="Color"> <option>White</option> <option>Blue</option> <option>Black</option> <option>Red</option> <option>Green</option> <option>Gold</option> <option>Colorless</option> </select> </label></td> <td><label> <select name="Type" id="Type"> <option>Land</option> <option>Instant</option> <option>Sorcery</option> <option>Artifact</option> <option>Enchantment</option> <option>Creature</option> <option>Planeswalker</option> </select> </label></td> <td><label> <select name="Rarity" id="Rarity"> <option>Common</option> <option>Uncommon</option> <option>Rare</option> <option>Mythic</option> </select> </label></td> <td><label> <input name="Price" type="text" id="Price" size="8" /> </label></td> <td><label> <input name="Stock" type="text" id="Stock" size="8" maxlength="3" /> </label></td> <td><label> <input name="Added" type="text" id="Added" size="10" maxlength="10" /> </label></td> </tr> <tr> <td colspan="7"><label> <div align="center"> <input type="submit" name="Submit" id="Submit" value="Submit" /> <input name="Submit" type="hidden" id="Submit" value="1" /> </div> </label></td> </tr> </table> </form> <p> </p> <table width="456" border="1"> <tr> <td>Name</td> <td>Color</td> <td>Type</td> <td>Rarity</td> <td>Price</td> <td>Stock</td> <td>Added</td> </tr> <?php do {?> <tr> <td><?php echo $row['Name'];?></td> <td><?php echo $row['Color'];?></td> <td><?php echo $row['Type'];?></td> <td><?php echo $row['Rarity'];?></td> <td><?php echo $row['Price'];?></td> <td><?php echo $row['Stock'];?></td> <td><?php echo $row['Added'];?></td> </tr> <?php }while ($row = mysql_fetch_array($result)); ?> </table> </body> </html> Link to comment https://forums.phpfreaks.com/topic/168066-small-problem-new-to-php/#findComment-887625 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.