Jump to content

Small problem / new to PHP


elindithas

Recommended Posts

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

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.");
}
?>

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?

<?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

<?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.

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>";
}
?>

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!

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.