Please help with this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Mayan
Forum Newbie
Posts: 3
Joined: Fri Jan 14, 2005 7:22 pm

Please help with this code

Post by Mayan »

Guys I am creating a big shopping cart....
the way the cart is being built might be strange... i am not sure
We have different categories of products... for example.. let say we have televisions... now the TVs have different categories like Plasma, tubes, LCDs and so on...
I created different folders labeled for the TV categories with all the php the files inside that link to one shopping cart...
now I am getting this error..on the CART.PHP file
Warning: extract(): First argument should be an array in C:\Program Files\YellowTip\Htdocs\SP\TMMC\Products\TubeTV\cart.php on line 62
It does however display the products... but only from whichever category I last added the product from....

This is GetRearProj.php File (All files that retrieve the product information are similar)

Code: Select all

<?php
session_start();
include ('../conn.inc.php');
$prodid=$_REQUEST&#1111;'prodid'];
$query = "SELECT * FROM RearProj WHERE prodnum='$prodid'";
$results = mysql_query($query)
     or die(mysql_error());
$row = mysql_fetch_array($results);
extract ($row);
?>
<HTML>
<HEAD>
<TITLE><?php echo $name ?></TITLE>
</HEAD>
<style fprolloverstyle>A:hover &#123;color: red; font-weight: bold&#125;
</style>
<BODY>

<div align="center">&nbsp;

<font face="arial"><h3><center>Select another item <strong><font color="blue"><?php echo $_SESSION&#1111;'user_logged']; ?></font></strong></center></h3>

<blockquote>
      <a style="text-decoration: none; color: #0000FF" href="../../Login/user_personal.php">&nbsp;Edit 
      Your Account&nbsp;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <a style="text-decoration: none; color: blue" href="../logout.php?action=Logout">
    <?php echo "Log out " . $_SESSION&#1111;'name'] . "</a>";?></font></p>
</blockquote>

</font>

</div>

<div align='center'><font face="arial">
<table width='500' cellpadding = '5'>
     <tr>
<?php
     echo "<td>";
     echo "<strong>";
     echo $name;
     echo "</strong><br>";
     echo $proddesc;
     echo "<br>";
     echo "Product Number: ";
     echo $prodnum;
     echo "<br>Price: ";
     echo $price;
     echo "</td>";
?>
     </tr>

     <tr>
       <td><form method="POST" action="add2.php">
     Quantity:
         <input type="text" name="qty" size="2">
         <input type="hidden" name="prodnum" value="<?php echo $prodnum ?>">
       <input type="submit" name="Submit" value="Add to cart">
     

       <form method = "POST" action="cart2.php">
     <input type="submit" name="Submit" value="View cart">
       </form>   </form>
       </td>
       </tr>

</table>
  <a style="text-decoration: none; color: #0000FF" href="RearProjShop.php">Back to Rear Projections</a></font> </div>
</BODY>
</HTML>
AND This here is CART.php file

Code: Select all

<?php
session_id();
session_start();
include "../auth_user.inc.php";

//connect to the database
$connect = mysql_connect("localhost", "admin", "asdfgh") or
     die ("The connection could not be established with the server.");
mysql_select_db ("TheMixedMediaCompany");
?>
<HTML>
<HEAD>
<TITLE>Lets Go Shopping!</TITLE>
</HEAD>
<BODY>

<p>
  <style fprolloverstyle>A:hover &#123;color: red; font-weight: bold&#125;
</style>
</p>
<p align="center">Welcome <strong><font color="blue"><?php echo $_SESSION&#1111;'user_logged']; ?></font></strong><font face="arial"> 
  </font> </p>
<blockquote>
  <blockquote>
<p align="center"><font color="#FF0000" face="arial"><a style="text-decoration: none; color: #0000FF" href="../../Login/user_personal.php">Edit 
      Your Account&nbsp;&nbsp;</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
      <a style="text-decoration: none; color: blue" href="logout.php?action=Logout">
    <?php echo "Log out " . $_SESSION&#1111;'name'] . "</a>";?></font></p>
  </blockquote>
</blockquote><br><br>
<font face="arial">
<div align="center">
  <p>You currently have 
    <?php
$sessid = session_id();

//display number of products in cart
     $query = "SELECT * from carttemp WHERE sess = '$sessid'";
     $results = mysql_query($query)
          or die (mysql_query());
     $rows = mysql_num_rows($results);
     echo $rows;
?>
    product(s) in your cart.</p>
  <table border="1" align="center" cellpadding="5">
      <tr>
           <td>Quantity</td>
           <!--<td>Item Image</td>-->
           <td>Item Name</td>
           <td>Price Each</td>
           <td>Extended Price</td>
           <td></td>
           <td></td>
      <tr>
      <?php
           while ($row = mysql_fetch_array($results)) &#123;
               extract ($row);
               $prod = "SELECT * FROM rearProj WHERE prodnum =
               '$prodnum'";
               $prod2 = mysql_query($prod);
               $prod3 = mysql_fetch_array($prod2);
               extract ($prod3); //**This is where its causing the problem***
               echo "<td><form method = 'POST' action='change2.php'>
                    <input type='hidden' name='prodnum'
                        value='$prodnum'>
                    <input type='hidden' name='sessid'
                        value='$sessid'>
                    <input type='hidden' name='hidden'
                        value='$hidden'>
                    <input type='text' name='qty' size='2'
                        value='$quan'>";
               echo "</td>";
               echo "<td>";
               echo "<a style='text-decoration: none; color: #0000FF' href = 'getRearProj.php?prodid=" .
                     $prodnum ."'>";
               echo $name;
               echo "</td></a>";
               echo "<td align='right'>";
               echo $price;
               echo "</td>";
               echo "<td align='right'>";
          //get extended price
               $extprice = number_format($price * $quan, 2);
               echo $extprice;
               echo "</td>";
               echo "<td>";
               echo "<input type='submit' name='Submit'
                         value='Change Qty'>
                      </form></td>";
               echo "<td>";
               echo "<form method = 'POST' action='delete2.php'>
                    <input type='hidden' name='prodnum'
                           value='$prodnum'>
                    <input type='hidden' name='qty' value='$quan'>
                    <input type='hidden' name='hidden'
                           value='$hidden'>
                    <input type='hidden' name='sessid'
                           value='$sessid'>";
               echo "<input type='submit' name='Submit'
                           value='Delete Item'>
                      </form></td>";
               echo "</tr>";
          //add extended price to total
               $total = $extprice + $total;

               &#125;
?>
<tr>
<td colspan='4' align='right'>Your total before shipping is:</td>
<td align='right'> <?php echo number_format($total, 2) ?></td>
<td></td>
<td></td>
</tr>
</table>
<form method="POST" action="../checkout.php">
<input type='submit' name='Submit' value='Checkout'>
</form>
  <a style="text-decoration: none; color: #0000FF" href="RearProjShop.php">Back to Rear Projection</a> </div>
</font>
</BODY>
</HTML>
Please advise what the problem might be and what I can do to fix it... thank you...[/quote]
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

The reason you are getting that warning it because your query is returning no rows. Its seems the reason it is only showing items from the rear projection category is because you have this table hard coded into your query:

Code: Select all

"SELECT * FROM rearProj WHERE prodnum =
               '$prodnum'";
You can fix the first problem by either not using extract or by validating that you got a result before using extract.

As for the second problem, I'm a little confiused as to why that is hard coded in there, but if you have a 'category' (or something) column in your 'carttemp' table you could do something like:

Code: Select all

"SELECT * FROM $category WHERE prodnum =
               '$prodnum'";
Although you are right that this does seem like a very strange way to build a shopping cart app.
Mayan
Forum Newbie
Posts: 3
Joined: Fri Jan 14, 2005 7:22 pm

Post by Mayan »

Thanks a lot yo... I am gonna try to mess around with the carttemp table and see if I go anywhere... in case of other suggestions.. please post them... or any advice...that would be great...
magicrobotmonkey
Forum Regular
Posts: 888
Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA

Post by magicrobotmonkey »

Well, from what you say it seems like you have a bunch of different directories each with their own .php file. This doesnt seem like a good way to do things for a variety of reasons. Firstly, anytime anything site-wide changes you have to go into every file and change it.
It also seems like you probably have separate tables for each category as well. Also not needed.

What you need to think about is the stuff you are selling, right? I mean most of it is probably pretty similar to the code. It has a price, a description, a picture, right? Those could all be columns in the table. The part that seems to have sent you astry, however, is the category-belonging-to of each item. Well, here's one solution. Have a 'category' table which contains rows like 'tv' 'dvd' 'other thing' etc. These rows will also have a field 'parent'. You also have rows like 'rear projection' and 'plasma', whose 'parent' is tv. In your items table there is a field category. All rear projections tvs are in the rear projection category. So when someone wants to browse categories, you display all categories without a parent as you know they are the main categories. Then, they pick 'tv' you get all the categories whose parent is tv. When they pick rear projection, they get all the items whos 'category' field is rear projection.


that's just one possible way...
Mayan
Forum Newbie
Posts: 3
Joined: Fri Jan 14, 2005 7:22 pm

Post by Mayan »

thanks man.. I'll try to do it that way....
thanks a lot
Post Reply