SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array within Foreach

    Hi guys

    I need to get the service[] array within the foreach.


    I get this error

    Warning: Invalid argument supplied for foreach() in timessetup.php on line 1043

    How can I fix this



    PHP Code:

    foreach($_POST['date'] as $row=>$Act)
    {
    $starttimehour=mysql_real_escape_string($_POST['starttime1'][$row]);
    $starttimeminute=mysql_real_escape_string($_POST['starttime2'][$row]);

    $services=mysql_real_escape_string($_POST['service'][$row]);
    echo
    "$services";

    foreach(
    $services as $s=>$Acts)
    {
    $sservices=mysql_real_escape_string($_POST['service'][$s]);

    echo
    "$sservices";








    Smarty page that sends the info

    PHP Code:
    {foreach from=$times_array item=module key=key}

    <
    tr>

    <
    td>
    {foreach 
    from=$service_data item=servicesitem key=key}
    <
    input type=checkbox name=service[] CHECKED value="{$servicesitem.Services_ID}"> <a href=editservice.php?id={$servicesitem.Services_IDtarget=new>{$servicesitem.Name}</a><br />
    {/foreach}
    </
    td>
    <
    td><input type=hidden name=date[] value="{$module|date_format:"%H:%M:%S"}"><input type=checkbox name=delete></td>
    </
    tr>




    {/foreach} 

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Since you haven't keyed these arrays at all in the form, you have no idea which set of service[] checkboxes go with which dates. You can't process that form unambiguously.

    Right now your loop is just pulling the first dates, the first service checkbox. Then the second dates, the second service checkbox. The third dates, then the third service checkbox. It doesn't know if those checkboxes went with those dates, or if all 3 went with the first date, or what.

  3. #3
    SitePoint Guru
    Join Date
    Oct 2004
    Location
    uk
    Posts
    853
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you, could you show me a example of how i can match these up?

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    <input type="checkbox" name="data[1][service]">
    <input type="text" name="data[1][date]">
    
    
    <input type="checkbox" name="data[55][service]">
    <input type="text" name="data[55][date]">
    the 1 and 55 can be any number or string. It just needs to be unique to each set of service/date pairs.

    Then you get a nice structured array in php that you can loop over
    PHP Code:
    print_r($_POST['data']); 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •