Jump to content

makamo66

Member
  • Posts

    18
  • Joined

  • Last visited

Posts posted by makamo66

  1. I have several hundred photos in my image directory because I was previously using ImageMagick and it resized photos every time the page was loaded. What I would like to know is are the images affecting my page loads? Will my site run slower when I have hundreds of images that aren't being loaded but are in the directory? These photos aren't being displayed on the page at all; they are just sitting in the directory.
     

  2. The var_dump of the $cart_items array is supposed to show an array of $cart_items with the quantities added together for the same id but it doesn't even show an array, it just shows one value.

    session_start();
    $cart_items = $_SESSION["cart_items"];
    if ( $cart_items == null ) {
        $cart_items = array();
    }
    if ( isset($_REQUEST["element_id"]) ) {
        $id = $_REQUEST["element_id"];
    }
    
    if ( isset($_REQUEST["quantity"]) ) {
        $quantity = $_REQUEST["quantity"];
    }
    
    if ( isset($id)  && isset($quantity) ) {
    if (isset($cart_items[$id])) {
              $cart_items[$id] += $quantity;
    
    } else {
        $cart_items[$id] = $quantity;
    }
    }
    var_dump($cart_items);

     
     

  3. The file test2.php consists of these simple forms:

    <?php
    
    for($i=0; $i<=5; $i++){
    
    echo "<form action='reset4.php' method='get' name='yourForm'>";
    
    echo "<button type='submit' value='delete' name='remove_" . $i . "' class='deletebtn'>X</button>";
    
    echo "</form>";
    
    }
    
    ?>
    

    It submits to reset4.php which is this simple code:

    <?php
    header("Location: test2.php");
    exit;
    
    
    for($i=0; $i<=5; $i++){
    if (isset($_REQUEST["remove_$i"])){
    echo "Deleted";
    }}
    
    ?>

    But it doesn't work. The $_REQUEST doesn't populate the address field and it apparently never gets submitted to reset4.php like it should. This is such a simple program, I can't imagine why it doesn't work. 

  4. This is the cart array:
    $cart = array( "1" => "2", "3" => "4", "5" => "6");
    var_dump($cart);
    array(3) { [1]=> string(1) "2" [3]=> string(1) "4" [5]=> string(1) "6" }
    This is the cart items array:
    $cart_items = array();
    $cart_items["id"] = $id;
    $cart_items["quantity"] = $quantity;
    var_dump($cart_items);
    array(2) { ["id"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } ["quantity"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "1" } }

    I want the key to be "id" and the value to be "quantity" like it is for the cart array. How do I define my cart_items array using variables?

  5. If you were working on a PHP shopping cart using SESSION variables for the cart item's product id and quantity, how would you update the cart if the same product were chosen? If the same product id was posted, how would you add the quantities and show just the same product?

  6. This is the code that I'm using for a shopping cart in order to update the quantity if the same product id is chosen. 

    $cart_items is a two-dimensional session array composed of the id session array and the quantity session array. While looping through $cart_items, I create a temporary array $temp_array so that I can execute some logic on the array without changing the normal output. $temp_array is the $cart_items array without the last items which would be the $_REQUEST id and the $_REQUEST quantity. So the two $_REQUESTs are popped off with array_pop and then the remaining $_SESSIONs are compared to the $_REQUEST id with 

    $j = array_search($_REQUEST["element_id_$i"], $temp_array);
    $j is now the key that corresponds to the id in $temp_array where the $_REQUEST id has been found. 

    if( $j==$temp_array[$i]) 

    If $j is the key of the item in $temp_array, then the $_REQUEST is unset and the quantities for the item are added together. 

    There are two problems with this code. First of all the var_dump of the $temp_array is always an empty array but I am expecting to get an array with just the last element popped off. Secondly, 
    I get the error "Undefined offset" for the $i variable in this line of the code: if( $j==$temp_array[$i]) 
     

    foreach($cart_items as $item) {
    foreach($item["id"] as $key => $val) {
    foreach($item["quantity"] as $i => $value) {
        if ($key == $i){
        
    $temp_array = $cart_items;
    $array = array_pop($temp_array);
    var_dump($temp_array);
    
    if (isset($_REQUEST["element_id_$i"]) && isset($_REQUEST["quantity_$i"])&& isset($value)){
        
    $j = array_search($_REQUEST["element_id_$i"], $temp_array);
    if( $j==$temp_array[$i]) {
    echo "<b>SAME PRODUCT ADDED</b>";
    $value += $_REQUEST["quantity_$i"];
    unset($_REQUEST["element_id_$i"]);
    }}}}}}

     

  7. <?php
    $_SESSION["cart_item"] = array(
    'cart_item' => array(
    'id' => $id,
    'product_name' => $product_name
    ));
    }
    $cart_items = $_SESSION["cart_item"];
    
    foreach ($cart_items as $cart_item) {
    echo $cart_item["id"] . $cart_item["product_name"];
    }
    ?>

    I have tried several variations of the foreach loop like the one above and I mostly get the error message: Notice: Array to string conversion. When I use:
    var_dump($cart_items);

    I get the following output:

    array(1) { ["cart_item"]=> array(2) { ["id"]=> array(2) { [0]=> string(1) "2" [1]=> string(1) "3" } ["product_name"]=> array(2) { [0]=> string(19) "Adult Female Bike" [1]=> string(18) "Kids Unisex Bike" } } }

  8. I want to use array_pop to remove the last item added to the SESSION array. So it would remove the REQUEST item. The problem is that nothing is left in the session array because I pop off the last item each time that the form is submitted. Maybe I need to use array_push to push the popped items back on to the SESSION array after the logic has been applied. I don't understand how array_pop is supposed to work on a SESSION array. Can anyone explain to me what behaviour to expect? I expected to get a SESSION array with the REQUEST item removed but I thought that I would still have all of my SESSION items.
     

  9. I changed the code so that it is using prepared statements but I just get new errors now. I altered the table so that the date_col column has the data type of varchar. I get the following warning:

    mysqli_stmt_bind_param(): Number of variables doesn't match number of parameters in prepared statement

    if(isset($_REQUEST["submit"]) ) {
    $date = new DateTime('', new DateTimeZone('America/New_York'));
    $name = mysqli_real_escape_string($connection, $_REQUEST["name"]);
    $date_col = $date->format('M d, Y H:i');
    $website = $_REQUEST["website"];
    $comment = mysqli_real_escape_string($connection, $_REQUEST["comment"]);
      
    $sql = "INSERT INTO comment_table (name,date_col,website,comment) VALUES ('$name', '$date_col','$website','$comment')";  
     
    if($stmt = mysqli_prepare($connection, $sql)){
        // Bind variables to the prepared statement as parameters
        mysqli_stmt_bind_param($stmt,'ssss',$name,$date_col,$website,$comment);
        // Attempt to execute the prepared statement
        if(mysqli_stmt_execute($stmt)){
            echo "Records inserted successfully.";
        } else{
            echo "ERROR: Could not execute query: $sql. " . mysqli_error($connection);
        }
    } else{
        echo "ERROR: Could not prepare query: $sql. " . mysqli_error($connection);
    }
     
    // Close statement
    mysqli_stmt_close($stmt);
    // Close connection
    mysqli_close($connection);

     

  10. I added a column to my database with SQL as follows:

    ALTER TABLE comment_table ADD date_col Datetime NOT NULL;

    I try to insert into the database with the following PHP but nothing gets inserted. 

    if(isset($_POST["submit"]))
    {

    $date_col = "test";//this will be DateTime later
    $name = $_POST["name"];

    mysqli_query($connection, "INSERT INTO comment_table (name, date_col) VALUES ('$name', '$date_col')"); 
    }
     
    $comsql = "SELECT * FROM comment_table";
    $comres = mysqli_query($connection, $comsql);
    while($comr = mysqli_fetch_assoc($comres)){
    ?>
    <div class="row">
    <p>Name: <strong><?php echo $comr['name']; ?></strong>This is the code that is being pointed to as undefined index. <?php echo $comr['date_col']; ?> </p>
    <?php } ?>
    </div>

    The data I'm using for the date_col column is varchar and it should be Datetime but I don't know if that's the reason for the error. I would like to set $date_col equal to a Datetime expression for testing purposes but the formatting I chose wasn't working either.

×
×
  • Create New...