Jump to content

rn14

Members
  • Posts

    111
  • Joined

  • Last visited

    Never

Posts posted by rn14

  1. Hi,

     

    I have an application where the user selects a number of people that they want to buy the product for.

    Based on this number their is a checkbox next to each product.

    The customer is then presented with 7 products of which they must select 5.

    I have written some simple if statements so that when the user selects the number of people this limits the the number of check boxes to the amount of people they have selected.

    To be clear, that means i have seven products on a page and if there are 3 people buying the products that will mean a total of 21 checkboxes. 7 for each person. Each person must select 5.

    My problem is trying to validate this to limit each person to picking 5 products:

    Here is what i have tryed:

     

    <?php
    session_start();
    $num_of_people = $_SESSION['numofpeople'];
    //meal for 1
    if($num_of_people == 1) {
    if (count($_POST['meal']) == 5) {
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
    }
    //meal for 2
    }elseif ($num_of_people == 2) {
    if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5) {
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
    }
    //meal for 3
    }elseif ($num_of_people == 3) {
    if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5 || count($_POST['meal3']) == 5) {
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
    }
    //meal for 4
    }elseif ($num_of_people == 4) {
    if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5 || count($_POST['meal3']) == 5 || count($_POST['meal4']) == 5) {
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
    }
    
    //meal for 5
    }elseif ($num_of_people == 5) {
    if (count($_POST['meal']) == 5 || count($_POST['meal2']) == 5 || count($_POST['meal3']) == 5 || count($_POST['meal4']) == 5 || count($_POST['meal5']) == 5) {
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustestw2.php">';
    }
    }else 
    echo '<META HTTP-EQUIV="Refresh" Content="0; URL=our_menustest.php">';
    
    ?>
    

     

    What happens above is that when a first person buying selects five products the validation does not work.

     

    Not sure if i explained this clearly.

     

    Thanks for any help

     

  2. I have 7 check boxes of which 5 and only 5 can be selected. These are printed in an array.

     

    These are my functions:

     

    <script type="text/javascript">
    function checkArray(mainform, arrayName) { var retval = new Array(); for(var i=0; i < form.elements.length; i++) { var el = form.elements[i]; if(el.type == "checkbox" && el.name == arrayName && el.checked) { retval.push(el.value); } } return retval; } 
    
    function checkForm(mainform) { var itemsChecked = checkArray(mainform, "meal[]"); alert("You selected " + itemsChecked.length + " items"); if(itemsChecked.length > 0) { alert("The items selected were:\n\t" + itemsChecked); } return false; } 
    </script>
    

     

    This is the form that i use

     

    
    <form method="post" action = "our_menustestw2.php" name = "mainform" onsubmit="checkForm();" >
    
    
    <input type="checkbox" name="meal2[]"  value="<?php echo $row['id']; ?>" CHECKED/> 
    
    <input type="submit" value="Submit Changes" name = "b1"> 
    
    
    

     

    Thanks in advance for any help

  3. Should this work????

     

    <?
    if($rows <= 5) {
    echo'<p><input type="checkbox" name="meal[]"  value="'.$row['id'].'" checked="checked"/>';
    }
    else {
    echo'<p><input type="checkbox" name="meal[]"  value="'.$row['id'].'" />';
    }
    ?>
    
    

     

    Thanks

  4. Its part of a while loop so will I be able to work it like that. I have to still print out the 7 checkboxes but just with 5 selected

     

    while($row = mysql_fetch_array($result))
    {
    $image = $row['image'];
    $title = $row['title'];
    $description = $row['text'];
    
    echo strip_tags($image, '<img>, <span>'); 
    ?>
    <p><input type="checkbox" name="meal[]"  value="<?php echo $row['id']; ?>" CHECKED/> 
    
    
    
    

     

    What you think??

  5. Hi,

     

    <p><input type="checkbox" name="meal[]"  value="<?php echo $row['id']; ?>" CHECKED/> 

     

    The above is printed out as part of a loop.

     

    there are 7 checkboxes printed out each one being checked. However i still want the seven printed out but with only the first 5 checked.

     

    Could anybody help me with this?

     

    Thanks

  6. if these are two objects that i have in sesssions how will I add them to the session array??

     

    if these are two objects that i have in sessions below How will I add them to the session array?
    
    session_start();
    include('class.php');
    
    if (isset($_SESSION['order'])) {
          $order = unserialize($_SESSION['order']);
    }else {
       $order = new Order();
    }
    // incase we want to reset our test data.
    if ($_REQUEST['reset'] == "ok") {
       $order = new Order();
    }
    $meallist = array();
    foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist = $v; } }
    $order->Test($meallist); //changed this to reference index of 0.
    $_SESSION['order'] = serialize($order);
    ?>
    
    <?
    //order +1
    if (isset($_SESSION['orderp2'])) {
          $orderp2 = unserialize($_SESSION['orderp2']);
    }else {
       $orderp2 = new Order();
    }
    // incase we want to reset our test data.
    if ($_REQUEST['reset'] == "ok") {
       $orderp2 = new Order();
    }
    $meallist = array();
    foreach ($_REQUEST as $k=>$v) { if ($k=="meal2") { $meallist = $v; } }
    $orderp2->Test($meallist); //changed this to reference index of 0.
    $_SESSION['orderp2'] = serialize($orderp2);
    ?>
    [code]
    
    Thanks

  7. This is where create a new object and pass values to it. I know it fills this with values as when i print out the values on this page they display.

     

    include('class.php');
    
    $order = new Order();
    $meallist = array();
    foreach ($_REQUEST as $k=>$v) { if ($k=="meal") { $meallist = $v; } }
    $order->Test($meallist); 
    

     

    My problem occurs when i attempt to print these values out on a different page using the following code:

     

    <?
    include('class.php');
    $array = $order->getData();
    $meal1 = $array[0];
    $meal2 = $array[1];
    $meal3 = $array[2];
    $meal4 = $array[3];
    $meal5 = $array[4];
    $meal6 = $array[5];
    $meal7 = $array[6];
    
    ?>
    

    I get this error:

    Fatal error: Call to a member function getData() on a non-object.

     

    Could anyone help?

     

     

    Thanks

  8. I have multiple objects each one is stored in a  separate session. Below I am unseriliazing the sessions and passing the values from each object to variables and then using them in a db query. The problem is the values I return from the first object are used as the values through all of the object I return on this page.

     

    Should I be able to this??

     

    Thanks

     

    <?
    session_start();
    include('class.php');
    
    $order = unserialize($_SESSION['order']);
    
    $array = $order->getData();
    $meal1 = $array[0];
    $meal2 = $array[1];
    $meal3 = $array[2];
    $meal4 = $array[3];
    $meal5 = $array[4];
    $meal6 = $array[5];
    $meal7 = $array[6];
    
    
    include('config.php');
    $link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)or die("Could not connect: " .
    mysql_error());
    mysql_select_db("_simp");
    if (isset($meal1)){
    $result = mysql_query("SELECT * FROM menus_show WHERE id = $meal1 ;");
    while($row = mysql_fetch_array($result))
    {
    $image = $row['imagesmall'];
    $title = $row['title'];
    $description = $row['text'];
    }
    
    
    }
    
    
    
    if (isset($meal2)){
    $result = mysql_query("SELECT * FROM menus_show WHERE id = $meal2 ;");
    while($row = mysql_fetch_array($result))
    {
    $image = $row['imagesmall'];
    $title = $row['title'];
    $description = $row['text'];
    }
    
    }
    
    session_unregister('$order');
    
    
    $orderw2 = unserialize($_SESSION['orderw2']);
    
    $array = $order->getData();
    $meal1 = $array[0];
    $meal2 = $array[1];
    $meal3 = $array[2];
    $meal4 = $array[3];
    $meal5 = $array[4];
    $meal6 = $array[5];
    $meal7 = $array[6];
    
    
    
    
    include('config.php');
    $link=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD)or die("Could not connect: " .
    mysql_error());
    mysql_select_db("_simp");
    if (isset($meal1)){
    $result = mysql_query("SELECT * FROM menus_show WHERE id = $meal1 ;");
    while($row = mysql_fetch_array($result))
    {
    $image = $row['imagesmall'];
    $title = $row['title'];
    $description = $row['text'];
    }
    }
    <?
    if (isset($meal2)){
    $result = mysql_query("SELECT * FROM menus_show WHERE id = $meal2 ;");
    while($row = mysql_fetch_array($result))
    {
    $image = $row['imagesmall'];
    $title = $row['title'];
    $description = $row['text'];
    }
    
    
    }
    

  9. im trying to learn oop at the moment could you explain the reasons for its poor design and how I can rectify them?

     

    When i use this

     $array = $order->getData();

    to access getData how will I extract the the different values in the array?

     

    Thanks and apologies for the basic and stupid questions

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