redarrow Posted October 7, 2008 Share Posted October 7, 2008 my loop not working it not looping the names......... <?php $self=$_SERVER['PHP_SELF']; $str.= "<table align='center' border='1'><tr><td align='center'><b>Name</b></td><td align='center'><b>Email Address</b></td><td align='center'><b>Add Contact</b></td>"; echo"<form method='POST' action='$self?cmd=add_contacts'>"; $contacts=array("name"=>"john","name"=>"sam","email"=>"john@lucky.co.uk","email"=>"sam@lucky.com"); for($i=0; $i<count($contacts); $i++){ $str.="<tr><td style='Font-Family:verdana;Font-Size:14'>".$contacts['name'][$i]."</td> <td style='Font-Family:verdana;Font-Size:14'>".$contacts['email'][$i]."<td> <input type='radio' name='add' value='yes'>Yes <input type='radio' checked='checked' name='add' value='no' >No <input type='hidden' name='contacts_name' value='".$contacts['name'][$i]."'> <input type='hidden' name='contacts_email' value='".$contacts['email'][$i]."'> </td></tr>"; } $str.= "</table><br><br> <input type='submit' name='add_contact' value='Add my contacts!'> <br><br>"; echo"<table border='0' width='800px' align='left'> <tr> <td align='center'> You have total <font color='blue'>$totalRecords</font> contacts <br><br> $str </td></tr> </table>"; if($_POST['add_contact']){ $add=$_POST['add']; $contacts_name=$_POST['contacts_name']; $contacts_email=$_POST['contacts_email']; if($_GET['cmd']=='add_contacts'){ echo " <br><br> Name: $contacts_name Email $contacts_email Add $add<br><br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/ Share on other sites More sharing options...
CroNiX Posted October 7, 2008 Share Posted October 7, 2008 You just mixed up your arrays.... instead of: $contacts['name'][$i] it should be: $contacts[$i]['name'] for all of the array elements. Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659041 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 does not work sorry <?php $self=$_SERVER['PHP_SELF']; $str.= "<table align='center' border='1'><tr><td align='center'><b>Name</b></td><td align='center'><b>Email Address</b></td><td align='center'><b>Add Contact</b></td>"; echo"<form method='POST' action='$self?cmd=add_contacts'>"; $contacts=array("name"=>"john","name"=>"sam","email"=>"john@lucky.co.uk","email"=>"sam@lucky.com"); for($i=0; $i<count($contacts); $i++){ $str.="<tr><td style='Font-Family:verdana;Font-Size:14'>".$contacts[$i]['name']."</td> <td style='Font-Family:verdana;Font-Size:14'>".$contacts[$i]['email']."<td> <input type='radio' name='add' value='yes'>Yes <input type='radio' checked='checked' name='add' value='no' >No <input type='hidden' name='contacts_name' value='".$contacts[$i]['name']."'> <input type='hidden' name='contacts_email' value='".$contacts[$i]['email']."'> </td></tr>"; } $str.= "</table><br><br> <input type='submit' name='add_contact' value='Add my contacts!'> <br><br>"; echo"<table border='0' width='800px' align='left'> <tr> <td align='center'> You have total <font color='blue'>$totalRecords</font> contacts <br><br> $str </td></tr> </table>"; if($_POST['add_contact']){ $add=$_POST['add']; $contacts_name=$_POST['contacts_name']; $contacts_email=$_POST['contacts_email']; if($_GET['cmd']=='add_contacts'){ echo " <br><br> Name: $contacts_name Email $contacts_email Add $add<br><br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659044 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 this dosent work iver....... <?php $self=$_SERVER['PHP_SELF']; $str.= "<table align='center' border='1'><tr><td align='center'><b>Name</b></td><td align='center'><b>Email Address</b></td><td align='center'><b>Add Contact</b></td>"; echo"<form method='POST' action='$self?cmd=add_contacts'>"; $contacts=array("name"=>"john","name"=>"sam","email"=>"john@lucky.co.uk","email"=>"sam@lucky.com"); for($i=0; $i<count($contacts); $i++){ $str.="<tr><td style='Font-Family:verdana;Font-Size:14'>".$contacts[$i['name']]."</td> <td style='Font-Family:verdana;Font-Size:14'>".$contacts[$i['email']]."<td> <input type='radio' name='add' value='yes'>Yes <input type='radio' checked='checked' name='add' value='no' >No <input type='hidden' name='contacts_name' value='".$contacts[$i['name']]."'> <input type='hidden' name='contacts_email' value='".$contacts[$i['email']]."'> </td></tr>"; } $str.= "</table><br><br> <input type='submit' name='add_contact' value='Add my contacts!'> <br><br>"; echo"<table border='0' width='800px' align='left'> <tr> <td align='center'> You have total <font color='blue'>$totalRecords</font> contacts <br><br> $str </td></tr> </table>"; if($_POST['add_contact']){ $add=$_POST['add']; $contacts_name=$_POST['contacts_name']; $contacts_email=$_POST['contacts_email']; if($_GET['cmd']=='add_contacts'){ echo " <br><br> Name: $contacts_name Email $contacts_email Add $add<br><br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659053 Share on other sites More sharing options...
Brandon Jaeger Posted October 7, 2008 Share Posted October 7, 2008 You need to have unique key names. You cannot repeat the same key name or else it'll output the last key and it's value and that's it. $contacts = array("john"=>"john@lucky.co.uk", "sam"=>"sam@lucky.com"); foreach($contacts as $name => $email) { $str.="<tr><td style='Font-Family:verdana;Font-Size:14'>".$name."</td> <td style='Font-Family:verdana;Font-Size:14'>".$email."<td> <td> <input type='radio' name='add' value='yes'>Yes <input type='radio' checked='checked' name='add' value='no' >No <input type='hidden' name='contacts_name' value='".$name."'> <input type='hidden' name='contacts_email' value='".$email."'> </td></tr>"; } Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659056 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 the problam is that the yes and no will move to yes and no to all records, how do u get it to only work on indvigal records.......... and thank you........ Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659065 Share on other sites More sharing options...
CroNiX Posted October 7, 2008 Share Posted October 7, 2008 I should have looked more closely.... This line: <?php $contacts=array("name"=>"john","name"=>"sam","email"=>"john@lucky.co.uk","email"=>"sam@lucky.com"); is wrong. You have 2 keys with the same index name, 'name' and 'email'. If you are wanting that to be 2 different people you should do something like: <?php $contacts=array(); $contacts[0]=array("name"=>"John", "email"=>"john@lucky.co.uk"); $contacts[1]=array("name"=>"Sam", "email"=>"sam@lucky.com"); It should work with your original code, my first correction and this one. then $contacts[1]["name"] will be "Sam" and so on Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659069 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 what can i do i want only to see the 2 entrys posted from the form currently 6 are shown...... <?php $self=$_SERVER['PHP_SELF']; $str.= "<table align='center' border='1'><tr><td align='center'><b>Name</b></t d><td align='center'><b>Email Address</b></td><td align='center'><b>Add Contact</b></td>"; echo"<form method='POST' action='$self?cmd=add_contacts'>"; $contacts = array("john"=>"john@lucky.co.uk", "sam"=>"sam@lucky.com"); foreach($contacts as $name => $email){ $str.="<tr><td style='Font-Family:verdana;Font-Size:14'>".$name."</td> <td style='Font-Family:verdana;Font-Size:14'>".$email."</td><td align='center'> <select name='add[]'><option value='no'>No<option value='yes'>Yes</option></select> <input type='hidden' name='contacts_name[]' value='".$name."'> <input type='hidden' name='contacts_email[]' value='".$email."'> </td></tr>"; } $str.= "</table><br><br><input type='submit' name='add_contact' value='Add my contacts!'> <br><br>"; echo"<table border='0' width='800px' align='left'> <tr> <td align='center'> You have total <font color='blue'>$totalRecords</font> contacts <br><br> $str </td></tr> </table>"; if($_POST['add_contact']){ $add=$_POST['add']; $contacts_name=$_POST['contacts_name']; $contacts_email=$_POST['contacts_email']; foreach($add as $a){ foreach($contacts_name as $cn){ foreach($contacts_email as $ce){ if($_GET['cmd']=='add_contacts'){ echo " <br><br> Name: $cn Email $ce Add $a<br><br>"; } } } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659137 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 can it be done posting a filled in form then select yes or no then post all the correct echoed form back within the same page.................... example below thank you............... Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659159 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 ive tried exit; never worked....... some how got to match the yes or no varable add to the existing array then post it then display it in correct order............ Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659167 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 LET ME EXSPLAIN THE PROBLAM TO EVERYONE BECOUSE IM PULLING MY HAIR OUT LOL.... 1. i log into a website ... via curl 2. i get back relevent info.... 3. the data sent back is let say name and email... 4. i add a current select box with yes or no beside the current collected info from curl... 5. the user see the info and selects yes or no.. 6. the form is postted with the relevent data from crul and the yes or no... 7. we only want to see the yes info only..... << not done yet..... the problam as you can see from the posted example below is when foreaching the info the yes or no has to be on the same line as the selected data the user selected.... very complecated....... so the quistion is how do i merge the select box with the current already loop showing the info...... Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659182 Share on other sites More sharing options...
sasa Posted October 7, 2008 Share Posted October 7, 2008 try <?php $self=$_SERVER['PHP_SELF']; $str.= "<table align='center' border='1'><tr><td align='center'><b>Name</b></t d><td align='center'><b>Email Address</b></td><td align='center'><b>Add Contact</b></td>"; echo"<form method='POST' action='$self?cmd=add_contacts'>"; $contacts = array("john"=>"john@lucky.co.uk", "sam"=>"sam@lucky.com"); foreach($contacts as $name => $email){ $str.="<tr><td style='Font-Family:verdana;Font-Size:14'>".$name."</td> <td style='Font-Family:verdana;Font-Size:14'>".$email."</td><td align='center'> <select name='add[]'><option value='no' selected='selected'>No<option value='yes'>Yes</option></select> <input type='hidden' name='contacts_name[]' value='".$name."'> <input type='hidden' name='contacts_email[]' value='".$email."'> </td></tr>"; } $str.= "</table><br><br><input type='submit' name='add_contact' value='Add my contacts!'> <br><br>"; echo"<table border='0' width='800px' align='left'> <tr> <td align='center'> You have total <font color='blue'>$totalRecords</font> contacts <br><br> $str </td></tr> </table>"; if($_POST['add_contact']){ $add=$_POST['add']; $contacts_name=$_POST['contacts_name']; $contacts_email=$_POST['contacts_email']; foreach($add as $key => $a){ if ($a == 'yes') echo " <br><br> Name: ",$contacts_name[$key]," Email ",$contacts_email[$key]," Add $a<br><br>"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659216 Share on other sites More sharing options...
redarrow Posted October 7, 2008 Author Share Posted October 7, 2008 YOU ARE MASTER LOL.............. Plese tell me where i went wrong ohhhhhhhhhh mighty one lol...................... and thank u......... Quote Link to comment https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/#findComment-659245 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.