Jump to content

Alexhoward

Members
  • Posts

    310
  • Joined

  • Last visited

    Never

Posts posted by Alexhoward

  1. Hi Guys,

     

    Having a bit of trouble here, not sure why it's not working, could anyone please help me out...?

     

    I'm trying to say that is a session class equals zero then unset the session,

     

    i think the problem may lie in the nested if..

     

    here's what i'm doing :

     

    <?php
     if($action == "add" ) {
     $_SESSION['a'][$product_id]++;
     }
     else if ($action == "remove" ) {
     $_SESSION['a'][$product_id]--;
     }
     else if($_SESSION['a'][$product_id] == 0 ) {
     unset($_SESSION['a'][$product_id]);
     }
     else if($action = "empty" ) {
     unset($_SESSION['mycart']);
     }
    ?>
    

     

    the problems with the unset not working..

     

    Thanks in advance

  2. hi,

     

    hold on.. it says something about hash tables, should i have a seperate file to save all this?

     

    i though the session would save it?

     

    sorry, but i'm now getting more errors...?

     

    here's the whole lot... i sure i'm just being a numpty.

     

    <?php
    
    include 'cart.class.php';
    
    session_start();
    
    include("config.php");
    
    //connect to the mysql server
    $link = mysql_connect($host, $db, $pass)
    or die ("Could not connect to mysql because ".mysql_error());
    
    //select the database	
    mysql_select_db($db)
    or die ("Could not select database because ".mysql_error());
    
    
         $product_id = $_GET['id'];     //the product id from the URL 
         $action     = $_GET['action']; //the action from the URL 
    
         //function to check if a product exists
         function productExists($product_id) {
                 //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
                 $sql = sprintf("SELECT id FROM products "); 
                     
                 return mysql_num_rows(mysql_query($sql)) > 0;
         }
    
         //if there is an product_id and that product_id doesn't exist display an error message
         if($product_id && !productExists($product_id)) {
             die("Error. Product Doesn't Exist");
         }
    
         switch($action) {    //decide what to do    
         
             case "add":
                 $_SESSION['cart'][$product_id] ++; //add one to the quantity of the product with id $product_id 
             break;
             
             case "remove":
                 $_SESSION['cart'][$product_id]--; //remove one from the quantity of the product with id $product_id 
                 if($_SESSION['cart'][$product_id] == 0) unset($_SESSION['cart'][$product_id]); //if the quantity is zero, remove it completely (using the 'unset' function) - otherwise is will show zero, then -1, -2 etc when the user keeps removing items. 
             break;
             
             case "empty":
                 unset($_SESSION['cart']); //unset the whole cart, i.e. empty the cart. 
             break;
         
         }
         
    ?>
    
    <?php    
    
         if($_SESSION['cart']) {    //if the cart isn't empty
             //show the cart
             
             echo "<table border=\"1\" padding=\"3\" width=\"40%\">";    //format the cart using a HTML table
             
                 //iterate through the cart, the $product_id is the key and $quantity is the value
                 foreach($_SESSION['cart'] as $product_id => $quantity) {    
                     
                     //get the name, description and price from the database - this will depend on your database implementation.
                     //use sprintf to make sure that $product_id is inserted into the query as a number - to prevent SQL injection
                     $sql = sprintf("SELECT name, description, price FROM php_shop_products WHERE id = %d;",
                                     $product_id); 
                         
                     $result = mysql_query($sql);
                         
                     //Only display the row if there is a product (though there should always be as we have already checked)
                     if(mysql_num_rows($result) > 0) {
                     
                         list($name, $description, $price) = mysql_fetch_row($result);
                     
                         $line_cost = $price * $quantity;        //work out the line cost
                         $total = $total + $line_cost;            //add to the total cost
                     
                         echo "<tr>";
                             //show this information in table cells
                             echo "<td align=\"center\">$name</td>";
                             //along with a 'remove' link next to the quantity - which links to this page, but with an action of remove, and the id of the current product
                             echo "<td align=\"center\">$quantity <a href=\"$_SERVER[php_SELF]?action=remove&id=$product_id\">X</a></td>";
                             echo "<td align=\"center\">$line_cost</td>";
                         
                         echo "</tr>";
                         
                     }
                 
                 }
                 
                 //show the total
                 echo "<tr>";
                     echo "<td colspan=\"2\" align=\"right\">Total</td>";
                     echo "<td align=\"right\">$total</td>";
                 echo "</tr>";
                 
                 //show the empty cart link - which links to this page, but with an action of empty. A simple bit of javascript in the onlick event of the link asks the user for confirmation
                 echo "<tr>";
                     echo "<td colspan=\"3\" align=\"right\"><a href=\"$_SERVER[php_SELF]?action=empty\" onclick=\"return confirm('Are you sure?');\">Empty Cart</a></td>";
                 echo "</tr>";        
             echo "</table>";
             
             
         
         }else{
             //otherwise tell the user they have no items in their cart
             echo "You have no items in your shopping cart.";
             
         } 
    ?>
    

     

    i'm now getting the errors:

     

    Warning: include(cart.class.php) [function.include]: failed to open stream: No such file or directory in E:\******\basket.php on line 3

     

    Warning: include() [function.include]: Failed opening 'cart.class.php' for inclusion (include_path='.;C:\php5\pear') in E:\******\basket.php on line 3

     

    Fatal error: Cannot use object of type __PHP_Incomplete_Class as array in E:\******\basket.php on line 37

     

    Thanks for all the help

     

  3. Sussed!

     

    This'll do the job...

     

    <?php
    
    while($row = mysql_fetch_array($sub_results)) { 
        $title = $row['title']; 
        $image1 = $row['image1'];
        $fdesc = SUBSTR($row['fdesc'],0,400);
        $user = $row['username'];
        $ref = $row['ref'];
    
    
         $image =  "$image1";                
         $size = getimagesize("$image");
           $height = $size[1];
           $width = $size[0];
         if ($height > $width)
              {
                   $height = 100;
                   $percent = ($size[1] / $height);
                   $width = ($size[0] / $percent);
              }
         else if ($width > $height)
              {
                   $width = 100;
                   $percent = ($size[0] / $width);
                   $height = ($size[1] / $percent);
              }
         //echo "<img src\"image/path/image.jpg\" height=\"$height\" width=\"$width\" />";
    
    ?>
    

  4. Hello guys,

     

    I've got this script from the PHP manual:

     

    <?php
    
    while($row = mysql_fetch_array($sub_results)) { 
        $title = $row['title']; 
        $image1 = $row['image1'];
        $fdesc = SUBSTR($row['fdesc'],0,400);
        $user = $row['username'];
        $ref = $row['ref'];
    
    
         $image =  "$image1";                
         $size = getimagesize("$image");
           $height = $size[1];
           $width = $size[0];
         if ($height > 100)
              {
                   $height = 100;
                   $percent = ($size[1] / $height);
                   $width = ($size[0] / $percent);
              }
         else if ($width > 100)
              {
                   $width = 100;
                   $percent = ($size[0] / $width);
                   $height = ($size[1] / $percent);
              }
         //echo "<img src\"image/path/image.jpg\" height=\"$height\" width=\"$width\" />";
    
    ?>
    

     

    The top part is the end of my select query...

     

    Problem is it's not working as i hoped.

     

    What i'd like to do is fit whatever sized image into a 100 X 100 cell,

     

    so i just assumed take whatever length is bigger and make it 100, then the other dimension would just scale proportionally.

     

    However this is not what it's doing.

     

    Could any of you suggest how i change this...?

     

    Thanks in advance!

  5. Hi Guys,

     

    Thanks for the replies!

     

    here's my code: (just a test and an extract of the full thing)

    <?php 
    
    
    include("config.php");
    
    //connect to the mysql server
    $link = mysql_connect($host, $db, $pass)
    or die ("Could not connect to mysql because ".mysql_error());
    
    //select the database	
    mysql_select_db($db)
    or die ("Could not select database because ".mysql_error());
    
    $query2=" SELECT * FROM mail ";
    $result2=mysql_query($query2);
    echo mysql_error();
    
    while($row = mysql_fetch_array($result2)) { 
        $from = $row['sender'];
        $title = $row['title'];
        $body = $row['body'];
        $date = $row['date'];
        $ref = $row['ref'];
    }
    
    echo "$date</br>";
    
    echo date("D-d-M-Y", $date);
    
    
    ?>
    

     

    problem being that my output is:

     

    2008-06-16 09:26:23

    Thu-01-Jan-1970

     

    any ideas why this is happening...?

     

    Thanks

  6. Good morning PHP people!

     

    I am using an auto timestamp in mysql 5.

     

    This gets added automatically when the data is inserted

     

    It's saved as:

     

    2008-06-16 09:09:53

     

    and i would like to change it to:

     

    Mon 16 June

     

    problem is that when i use the normal php date formatting method it comes out with some bizare results...

     

    has anyone come across this before...?

     

    Thanks

  7. Hi Guys,

     

    Thanks for taking the time to look at this

     

    <?php
    
    //Select Category
    
    include("config.php");
    
    //connect to the mysql server
    $link = mysql_connect($host, $db, $pass)
    or die ("Could not connect to mysql because ".mysql_error());
    
    //select the database	
    mysql_select_db($db)
    or die ("Could not select database because ".mysql_error());
    
    
    echo'<form><select name="cat" style="width:160px;">';
    
    $res=mysql_query("select distinct cat from category WHERE cat !='Add_Category..?' order by cat");
    if(mysql_num_rows($res)==0) echo "there is no data in table..";
    else
    for($i=0;$i<mysql_num_rows($res);$i++) {
    $row=mysql_fetch_assoc($res);
    $selected = @$_GET['cat'] == $row['cat'] ? "selected" : "";
    echo"<option value=".$row['cat']." $selected>".$row['cat']."</option>";
    }
    echo'</select>';
    echo'<input type="submit" value="select"></form>';
    
    ?>
    

  8. Hello!

     

    so i know how to pull a drop down box from mysql.

     

    thing is i would like to have a start value that is not in the table

     

    e.g.

     

    say i'm pulling from a category table.

     

    this may include - Home, Electronics, Games ....

     

    i would like people to be able to add their own category if it does fit into any of those groups

     

    so i would like to include "Add_Category..?" as an option

     

    could anyone help me?

     

    Thanks in advance!

  9. Hi Guys,

     

    I have a drop down populated from mysql...

     

    (these things have taken over all my time lately!  :D )

     

    Some how thou i would like to ignore certain values.

     

    e.g. say the table includes - a, b, c, d, e ...

     

    I would like to pull everything back apart from d

     

    can anyone help?

     

    Thanks

  10. Cheers mate,

     

    This is how i'm doing it :

     

    <?php
    
    $query=""; 
    $query=$query."cat=\"$_GET[cat]\" ";
    
    if(isset($_GET['subcat'])){
    $query=$query." AND subcat=\"$_GET[subcat]\" ";
    }
    if ($query == "") { $query = 1; } else {                      
        $sub_query = "SELECT * FROM idea WHERE ".$query. "  ORDER BY ref DESC";
        if(strlen($_GET['cat'])==0){
    $sub_query = "SELECT * FROM idea ORDER BY ref DESC";
        } 
            $sub_results = mysql_query($sub_query);
            if (!$sub_results) {
                $message  = 'Invalid query: ' . mysql_error() . "\n";
                $message .= 'Whole query: ' . $sub_query;
                die($message);
            }
    }
    
    ?>
    

     

    Thanks for your help!

  11. Hi Guys,

     

    This ones really got me...

     

    I've got two drop down's populated from mysql; cat and subcat, which both post to the page. e.g www.website.com?cat=cat

     

    They are on two seperate forms as subcat is poulated from $_GET['cat']

     

    subcat has a hidden input containing cat so that when you post the subcat selection it also posts cat

    e.g. www.website.com?cat=cat&subcat=subcat

     

    I would then like to pull back the info out of my table based on these variables

     

    so, i'm trying something like:

     

    <?php
    $query2=" SELECT * FROM table ".$_GET['cat']." ".$_GET['subcat']." ORDER BY ref DESC ";
    $result2=mysql_query($query2);
    ?>
    

     

    problem is i have to set the variables if they are not, else you get an error

     

    so:

     

    <?php
    if(!isset($_GET['cat'])) { $_GET['cat'] = " "; }
    if(!isset($_GET['subcat'])) { $_GET['subcat'] = " "; }
    
    
    if(isset($_GET['cat'])) { $_GET['cat'] = "  WHERE cat = '".$_GET['cat']."'"; }
    if(isset($_GET['subcat'])) { $_GET['subcat'] = " AND subcat = '".$_GET['subcat']."'"; }
    
    
    $query2=" SELECT * FROM idea ".$_GET['cat']." ".$_GET['subcat']." ORDER BY ref DESC ";
    $result2=mysql_query($query2);
    ?>
    

     

    What i'm trying to achieve is to set them as nothing if they are not set, so the query will pull back everything from the table, problem is i am setting them by doing this, so when unselected cat will equal "WHERE cat = " and subcat will equal "AND subcat = "

     

    Another issue is that i also have to do this for the the subcat dropdown as it has cat as a hidden input, so i'm already setting it there too...!

     

    I seem to be stuck in this horrible loop!

     

    can anyone help me out, or suggest anything?

     

    Thanks is advance for taking the time to read this

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