Page 1 of 1
if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:01 am
by ganza
hi there,
i have the code foreach that read data from mysql table and show it in echo
but is it possible for me to make the edit button working?
because im going to make like this: if editpic button pressed
the input text will come out
and will update the personincharge with the text in input text WHERE contact_ID="'.$record['contact_ID'].'"
Code: Select all
foreach($list as $record)
{
echo "<tr>";
echo "<td>".$record['personIncharge']."<input type='submit' name='EditPIC' value='edit'>";
echo "</td>";
echo "<td>".$record['designation']."</td>";
echo "<td>".$record['department']."</td>";
echo "<td>".$record['mainLine']."</td>";
echo "<td>".$record['faxLine']."</td>";
echo "<td>".$record['directLine']."</td>";
echo "<td>".$record['mobile']."</td>";
echo "<td>";
echo '<a href="deleteRow.php?id=' . $record['contact_ID'] . '">Delete</a>';
echo "</td>";
echo "</tr>";
}
echo '</table>';
echo "</form>";
}
is it possible for me to make the button work?
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:14 am
by aceconcepts
You'll need to create a hidden input value to post when teh edit button is clicked.
You can then use isset() to determine whether the "Edit" button has been clicked.
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:18 am
by ganza
aceconcepts wrote:You'll need to create a hidden input value to post when teh edit button is clicked.
You can then use isset() to determine whether the "Edit" button has been clicked.
where should i write the hidden value? inside the foreach()?
if you dont mind is it possible to write an example code?

Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:30 am
by aceconcepts
Notice the hidden form element with it's value:
Code: Select all
echo '<td>'.$record['personIncharge'].'<input type="submit" name="EditPIC" value="edit"><input type="hidden" name="contact_ID[]" value="'.$record['contact_ID'].'" />';
echo "</td>";
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:41 am
by ganza
ok i make a code like this ....
but the if statement inside foreach is not working
when i click edit... it wont show the input text to write new personIncharge
Code: Select all
if(isset($_REQUEST["Submit"]) == true){
$contactList = "SELECT * FROM contact WHERE client_ID = ('".$_REQUEST["client_ID"]."')";
$db->sql_query($contactList);
$list = $db->sql_fetchrowset();
echo "<form id=\"delContact\" name=\"delContact\" method=\"post\" action=\"\">";
echo '<table border=1>';
echo "<tr>";
echo "<td>Person in Charge</td>";
echo "<td>Designation</td>";
echo "<td>Department</td>";
echo "<td>Main Line</td>";
echo "<td>Fax Line</td>";
echo "<td>Direct Line</td>";
echo "<td>Mobile</td>";
echo "</tr>";
foreach($list as $record)
{
echo "<tr>";
echo "<form id=\"editContact\" name=\"editContact\" method=\"post\" action=\"\">";
echo "<td>".$record['personIncharge'].'<input type="submit" name="EditPIC" value="edit"><input type="hidden" name="contact_ID[]" value="'.$record['contact_ID'].'"/>';
echo "</form>";
echo "<td>".$record['designation']."</td>";
echo "<td>".$record['department']."</td>";
echo "<td>".$record['mainLine']."</td>";
echo "<td>".$record['faxLine']."</td>";
echo "<td>".$record['directLine']."</td>";
echo "<td>".$record['mobile']."</td>";
echo "<td>";
echo '<a href="deleteRow.php?id='.$record['contact_ID'].'">Delete</a>';
echo "</td>";
echo "</tr>";
if(isset($_REQUEST["EditPIC"]) == true){
echo "<input name='editPIC' id='editPIC' type='text'>";
$editPIC = "UPDATE contact SET personIncharge='".$_REQUEST["editPIC"]."' WHERE contact_ID='".$record['contact_ID']."'";
$db->sql_query($editPIC);
}
}
echo '</table>';
echo "</form>";
}
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:49 am
by aceconcepts
isset() returns TRUE or FALSE - see
http://uk2.php.net/isset
So you can write it like this:
$_REQUEST[] is used to GET values from forms using the GET method or from the url. Your form uses the POST method - so use $_POST[].
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:52 am
by ganza
still wont working.
the <input type="text"> still wont come out /....
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 4:57 am
by aceconcepts
I just noticed, you have a form within a form - this wont work. Create one form.
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 5:05 am
by ganza
aceconcepts wrote:I just noticed, you have a form within a form - this wont work. Create one form.
i've deleted the form but still wont work .....
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 6:46 am
by aceconcepts
Can you show your most recent script?
Re: if statement inside foreach() ?
Posted: Tue Nov 11, 2008 9:23 am
by ganza
aceconcepts wrote:Can you show your most recent script?
ok this is the code ...
Code: Select all
if(isset($_POST["EditPIC"])){
echo "<input name='editPIC' id='editPIC' type='text'>";
echo "<form id='editPIC' name='editPIC' method='post' action=\"\">";
echo "<input name='submitPIC' id='submitPIC' type='submit' value="update!">";
echo "</form>";
}
if(isset($_POST["submitPIC"])){
$editPIC = "UPDATE contact SET personIncharge='".mysql_real_escape_string($_POST["editPIC"])."' WHERE contact_ID='".$_POST['contact_ID']."'";
$db->sql_query($editPIC);
}
if(isset($_REQUEST["Submit"]) == true){
$contactList = "SELECT * FROM contact WHERE client_ID = ('".$_REQUEST["client_ID"]."')";
$db->sql_query($contactList);
$list = $db->sql_fetchrowset();
echo "<form id=\"delContact\" name=\"delContact\" method=\"post\" action=\"\">";
echo '<table border=1>';
echo "<tr>";
echo "<td>Person in Charge</td>";
echo "<td>Designation</td>";
echo "<td>Department</td>";
echo "<td>Main Line</td>";
echo "<td>Fax Line</td>";
echo "<td>Direct Line</td>";
echo "<td>Mobile</td>";
echo "</tr>";
foreach($list as $record)
{
echo "<tr>";
echo "<td>".$record['personIncharge'].'<input type="submit" name="EditPIC" value="edit"><input type="hidden" name="contact_ID[]" value="'.$record['contact_ID'].'"/>';
echo "<td>".$record['designation']."</td>";
echo "<td>".$record['department']."</td>";
echo "<td>".$record['mainLine']."</td>";
echo "<td>".$record['faxLine']."</td>";
echo "<td>".$record['directLine']."</td>";
echo "<td>".$record['mobile']."</td>";
echo "<td>";
echo '<a href="deleteRow.php?id='.$record['contact_ID'].'">Delete</a>';
echo "</td>";
echo "</tr>";
}
echo '</table>';
echo "</form>";
}
the code run perfectly until the textfield comeout after the edit button pressed
but after i write the new personIncharge value on textfield and pressed update! button.
the database didnt update anything
i think because it cannot get '".$_POST['contact_ID']."' ....
is there anyone can help check the code?

Re: if statement inside foreach() ?
Posted: Wed Nov 12, 2008 1:14 am
by ganza
is there anyone can tell me how to make the $_POST['contact_ID'] work?