dwest Posted January 23, 2007 Share Posted January 23, 2007 [b]This code...[/b][code]<?phpforeach ($_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]3Professional Website DevelopmentAnnual Website Hosting4Professional Website DevelopmentAnnual Website Hosting[b]Instead of this, which is what I want...[/b]3Professional Website Development4Annual Website Hosting[b]What am I doing wrong??[/b] Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/ Share on other sites More sharing options...
bqallover Posted January 23, 2007 Share Posted January 23, 2007 Your code is working logically as one would expect. The inner foreach will run exactly the same each time, giving you the same results. Can you post the corresponding HTML form section so we can get a better picture of what you're trying to do? Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-166994 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-166998 Share on other sites More sharing options...
bqallover Posted January 23, 2007 Share Posted January 23, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167007 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 Well, it sort of works but I don't understand the output which is:3A4nAny ideas? Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167010 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 Ooooops!Sorry!Hang in there a minute. I removed the [] while tinkering.Give me a minute to re-test... Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167011 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 BINGO!Works like a charm now.Many, many thanks!! Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167012 Share on other sites More sharing options...
bqallover Posted January 23, 2007 Share Posted January 23, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167013 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 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 Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167015 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 You're right on your correction...if one wants to pull the data from a database on the receiving page. In this case I didn't.Don't ask why...it's complicated and I'm already in a confused state :) Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167018 Share on other sites More sharing options...
bqallover Posted January 23, 2007 Share Posted January 23, 2007 [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? Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167021 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 EDITED: this is based on the non-query solution...your first solution...[code]<?phpfor( $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]3Professional Website Developmentcomplete website development as described in the development agreement1225.004Annual Website Hosting12 months of website hosting and technical support1225.00[b]Select first Item in the list[/b]3Professional Website Developmentcomplete website development as described in the development agreement1225.00[b]Select second item in the list[/b]4Professional Website Developmentcomplete website development as described in the development agreement1225.00Only the item number is correct.Should be:4Annual Website Hosting12 months of website hosting and technical support1225.00You're on the right track though. Just a tweak is needed I think.Any idea how to fix it? Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167023 Share on other sites More sharing options...
bqallover Posted January 23, 2007 Share Posted January 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167025 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167029 Share on other sites More sharing options...
bqallover Posted January 23, 2007 Share Posted January 23, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167032 Share on other sites More sharing options...
dwest Posted January 23, 2007 Author Share Posted January 23, 2007 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. :) Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167034 Share on other sites More sharing options...
bqallover Posted January 23, 2007 Share Posted January 23, 2007 Good luck - and thanks for getting me over the 100 mark! ;)<---- Quote Link to comment https://forums.phpfreaks.com/topic/35333-nested-for-each-question/#findComment-167038 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.