Jump to content

loop not working please help cheers.......


redarrow

Recommended Posts

 

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"=>"[email protected]","email"=>"[email protected]");


		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>";

}
}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/127408-loop-not-working-please-help-cheers/
Share on other sites

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"=>"[email protected]","email"=>"[email protected]");


		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>";

}
}

?>

 

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"=>"[email protected]","email"=>"[email protected]");


		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>";

}
}

?>

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"=>"[email protected]", "sam"=>"[email protected]");

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>";
}

I should have looked more closely....

This line:

<?php $contacts=array("name"=>"john","name"=>"sam","email"=>"[email protected]","email"=>"[email protected]");

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"=>"[email protected]");
$contacts[1]=array("name"=>"Sam", "email"=>"[email protected]");

It should work with your original code, my first correction and this one.

then $contacts[1]["name"] will be "Sam" and so on

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"=>"[email protected]", "sam"=>"[email protected]");

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>";

}
}
  }
   }
    }
?>

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

 

 

 

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"=>"[email protected]", "sam"=>"[email protected]");
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>";
}
}
?>

Archived

This topic is now archived and is closed to further replies.

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