if statement inside foreach() ?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ganza
Forum Newbie
Posts: 19
Joined: Fri Nov 07, 2008 3:56 am

if statement inside foreach() ?

Post 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?
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: if statement inside foreach() ?

Post 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.
ganza
Forum Newbie
Posts: 19
Joined: Fri Nov 07, 2008 3:56 am

Re: if statement inside foreach() ?

Post 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? :oops:
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: if statement inside foreach() ?

Post 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>";
 
ganza
Forum Newbie
Posts: 19
Joined: Fri Nov 07, 2008 3:56 am

Re: if statement inside foreach() ?

Post 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>";
} 
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: if statement inside foreach() ?

Post by aceconcepts »

isset() returns TRUE or FALSE - see http://uk2.php.net/isset

So you can write it like this:

Code: Select all

if(isset($_POST['EditPic']))
$_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[].
ganza
Forum Newbie
Posts: 19
Joined: Fri Nov 07, 2008 3:56 am

Re: if statement inside foreach() ?

Post by ganza »

still wont working.
the <input type="text"> still wont come out /....
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: if statement inside foreach() ?

Post by aceconcepts »

I just noticed, you have a form within a form - this wont work. Create one form.
ganza
Forum Newbie
Posts: 19
Joined: Fri Nov 07, 2008 3:56 am

Re: if statement inside foreach() ?

Post 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 .....
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: if statement inside foreach() ?

Post by aceconcepts »

Can you show your most recent script?
ganza
Forum Newbie
Posts: 19
Joined: Fri Nov 07, 2008 3:56 am

Re: if statement inside foreach() ?

Post 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? :crazy: :banghead:
ganza
Forum Newbie
Posts: 19
Joined: Fri Nov 07, 2008 3:56 am

Re: if statement inside foreach() ?

Post by ganza »

is there anyone can tell me how to make the $_POST['contact_ID'] work?
Post Reply