Jump to content

Nested for each question


dwest

Recommended Posts

[b]This code...[/b]
[code]
<?php

foreach ($_POST['id'] as $id)
    {
        echo $id.'<br />';
            foreach ($_POST['hid_name'] as $hid_name)
                {
                  echo $hid_name.'<br />';
                }
    }

?>
[/code]

[b]Is giving me this...[/b]

3
Professional Website Development
Annual Website Hosting
4
Professional Website Development
Annual Website Hosting

[b]Instead of this, which is what I want...[/b]

3
Professional Website Development
4
Annual Website Hosting

[b]What am I doing wrong??[/b]
Link to comment
Share on other sites

hehe...I was trying to keep the post simple.  Here is the part of the form that is posted.

[code]
$query = mysql_query("SELECT item_id, name, descr, qty, price FROM tbl_items");
    while($r = mysql_fetch_array($query))
        {
            echo'
                <tr>
                <td><input type="checkbox" name="id[]" value="'.$r['item_id'].'"></td>
                <td>'.$name = $r['name'].'<input type="hidden" name="hid_name[]" value="'.$r['name'].'" /></td>
                <td>'.$descr = $r['descr'].'<input type="hidden" name="hid_descr[]" value="'.$r['descr'].'" /></td>
                <td>'.$qty = $r['qty'].'<input type="hidden" name="hid_qty[]" value="'.$r['qty'].'" /></td>
                <td>'.$price = $r['price'].'<input type="hidden" name="hid_price[]" value="'.$r['price'].'" /></td>
                </tr>';
    }

[/code]

Link to comment
Share on other sites

I see.

Try this instead of the double-foreach block you posted.  Not 100% sure if it'll work but hey... :)  It's a bit of a kludge.

[code]
for( $x = 0; $x < count( $_POST['id'] ); $x++ )
{
    echo $_POST['id'][$x] . '<br />';
    echo $_POST['hid_name'][$x] . '<br />';
}
[/code]

edit:  Actually sorry, I don't think that'll work at all.  Let me think...
Link to comment
Share on other sites

Sorry, this is the right way to do this!  <slaps own head>

[code]
foreach( $_POST['id'] as $id )
{
  $query = mysql_query("SELECT name FROM tbl_items WHERE item_id='$id'");
    while($r = mysql_fetch_array($query))
    {
        echo $id . '<br />';
        echo $r['name'] . '<br />';
    }
}
[/code]

Link to comment
Share on other sites

[quote author=dwest link=topic=123624.msg511199#msg511199 date=1169543362]
For the sake of myself and other neophytes, could you explain what is going on with your solution?
I'd like to learn how to arrive at these answers on my own  :)
[/quote]

Which one?  :)  Actually can you post the fixed version that worked for you?
Link to comment
Share on other sites

EDITED: this is based on the non-query solution...your first solution...
[code]
<?php
for( $x = 0; $x < count( $_POST['id'] ); $x++ )
{
    echo $_POST['id'][$x] . '<br />';
    echo $_POST['hid_name'][$x] . '<br />';
    echo $_POST['hid_descr'][$x] . '<br />';
    echo $_POST['hid_qty'][$x] . '<br />';
    echo $_POST['hid_price'][$x] . '<br />';
}
?>
[/code]

hah! what a mess I've come up with :-\

Your original solution sort of works.  If I select the second record only, the item number changes but the corresponding data does not.

Here is the print out of each scenario, note that I added the other fields to the result:

[b]Select both items in the list:[/b]

3
Professional Website Development
complete website development as described in the development agreement
1
225.00
4
Annual Website Hosting
12 months of website hosting and technical support
1
225.00

[b]Select first Item in the list[/b]
3
Professional Website Development
complete website development as described in the development agreement
1
225.00

[b]Select second item in the list[/b]
4
Professional Website Development
complete website development as described in the development agreement
1
225.00

Only the item number is correct.
Should be:
4
Annual Website Hosting
12 months of website hosting and technical support
1
225.00

You're on the right track though.  Just a tweak is needed I think.
Any idea how to fix it?
Link to comment
Share on other sites

LOL.  This is going to be so ugly it's unreal...

[code]
for( $x = 0; $x < count( $_POST['id'] ); $x++ )
{
    $id = $_POST['id'][$x];
    echo $id . '<br />';
    echo $_POST['hid_name'][$id] . '<br />';
}
[/code]

I'm going to have a red face if someone points out an obvious way of doing this.  The DB way REALLY is the best as all these other solutions assume I know exactly what you're doing, which I don't. :)  Try it anyway.
Link to comment
Share on other sites

hmmmm.
No we're still getting the record one data with the record two id if I select only record two.

I agree on the database solution.  I would like to see this work though. In essence, it eliminates a second call to the database in the receiving page if nothing else.  I know I have a solid reason for needing it, though I'm so dang confused now I can't remember what it was.

Don't spend much time tweaking further unless the answer is obvious to you.

Isn't working with us nim cum poops fun?  ;D
Link to comment
Share on other sites

Ok, one last throw of the dice.  This is even WORSE than the above suggestion, but it might just work. :)

Replace your form code with this...

[code]
$query = mysql_query("SELECT item_id, name, descr, qty, price FROM tbl_items");
while($r = mysql_fetch_array($query))
{
$id = $r['item_id'];
echo'
<tr>
<td><input type="checkbox" name="id[]" value="'.$id.'"></td>
<td>'.$name = $r['name'].'<input type="hidden" name="hid_name_'.$id.'" value="'.$r['name'].'" /></td>
<td>'.$descr = $r['descr'].'<input type="hidden" name="hid_descr_'.$id.'" value="'.$r['descr'].'" /></td>
<td>'.$qty = $r['qty'].'<input type="hidden" name="hid_qty_'.$id.'" value="'.$r['qty'].'" /></td>
<td>'.$price = $r['price'].'<input type="hidden" name="hid_price_'.$id.'" value="'.$r['price'].'" /></td>
</tr>';
}
[/code]

Then replace your foreach with this...

[code]
for( $x = 0; $x < count( $_POST['id'] ); $x++ )
{
    $id = $_POST['id'][$x];
    echo $id . '<br />';
    echo $_POST['hid_name_'.$id] . '<br />';
    echo $_POST['hid_descr_'.$id] . '<br />';
    echo $_POST['hid_qty_'.$id] . '<br />';
    echo $_POST['hid_price_'.$id] . '<br />';
}
[/code]
Link to comment
Share on other sites

All we get now is the id as the result.  No other parameters are echoing.

Let's stop the torture :)
I TRULY appreciate your efforts!

I will approach this from the database perspective...I know how to deal with that.

I'll create a new post that addresses the overall problem I'm dealing with.

Thanks again!!!

You are a Saint.  :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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