Page 1 of 1

Forum Iteration

Posted: Sat Jul 22, 2006 7:26 pm
by SidewinderX
ok, ive created a simple code but its not EXACTLEY what i want, maybe someone can help me.

Code: Select all

<?php
$value = "5";
$db = mysql_connect("", "", "");
mysql_select_db("bhdsig",$db);
$result = mysql_query("SELECT * FROM pid ORDER BY pid", $db)or die("Error: ".mysql_error()); 
while ($row = mysql_fetch_assoc($result))
{ 
$pid = $row['pid'];
echo "<FORM NAME='Name_01' ACTION='options.php' METHOD='POST'> 
<input type='hidden' NAME='S1' VALUE='". $value ."'>
<input type='hidden' NAME='S2' VALUE='". $value ."'>
<input type='hidden' NAME='S3' VALUE='". $value ."'>
<input type='hidden' NAME='S4' VALUE='". $value ."'>
<input type='hidden' NAME='S5' VALUE='". $value ."'>
<input type='hidden' NAME='S6' VALUE='". $value ."'>
<input type='hidden' NAME='S7' VALUE='". $value ."'>
<input type='hidden' NAME='S8' VALUE='". $value ."'>
<input type='hidden' NAME='S9' VALUE='". $value ."'>
<input type='hidden' NAME='Background' VALUE='Afghanistan'>
<input type='hidden' NAME='TextColor' VALUE='Black'>
<input type='hidden' NAME='FontStyle' VALUE='Arial'>
<input type='hidden' name='oncethrough' value='Yes'>
<input type='hidden' name='playerid' value='". $pid ."'>
<input class='black' type='submit' name='submit' value='Change The Sig'>
</FORM>";
}
?>
currently what this is doing is making a new form for each $pid in the database, thats not what i want.
I want to press the "Change The Sig" once button and it iterate the same script for all $pid's in the database, i'd say it has something to do with the placement of the loop, but im not sure where exactley where to place it.

Any help is appreciated.
Thank You.

Posted: Sat Jul 22, 2006 7:35 pm
by Burrito
everything within your while{} will get duplicated for each iteration of the loop.

if you don't want stuff duplicated, pull it out of the while{} and put it before or after.

Posted: Sat Jul 22, 2006 7:42 pm
by SidewinderX
thank you for your reply, but i am aware of that. however, im unsure what i want to be duplicated to accomplish this task

Posted: Sat Jul 22, 2006 7:44 pm
by Burrito
then you're going to need to restate your orignial question as I am very confused when I read it...

Posted: Sat Jul 22, 2006 8:07 pm
by SidewinderX
hm ok ill try...

the code as is displays 10 different submit buttons, 1 submit button for each value in the database, and of course this is because the entire code is looped....

I just want 1 submit button that when pressed will post this information to options.php for each value in the database


<input type='hidden' NAME='S1' VALUE='". $value ."'>
<input type='hidden' NAME='S2' VALUE='". $value ."'>
<input type='hidden' NAME='S3' VALUE='". $value ."'>
<input type='hidden' NAME='S4' VALUE='". $value ."'>
<input type='hidden' NAME='S5' VALUE='". $value ."'>
<input type='hidden' NAME='S6' VALUE='". $value ."'>
<input type='hidden' NAME='S7' VALUE='". $value ."'>
<input type='hidden' NAME='S8' VALUE='". $value ."'>
<input type='hidden' NAME='S9' VALUE='". $value ."'>
<input type='hidden' NAME='Background' VALUE='Afghanistan'>
<input type='hidden' NAME='TextColor' VALUE='Black'>
<input type='hidden' NAME='FontStyle' VALUE='Arial'>
<input type='hidden' name='oncethrough' value='Yes'>

Posted: Sat Jul 22, 2006 8:18 pm
by Benjamin
What are the field names in the table?

Posted: Sat Jul 22, 2006 8:19 pm
by feyd
so move the submit button out of the loop.

Posted: Sat Jul 22, 2006 8:38 pm
by Benjamin
This is a quick example of what I think your trying to do.. I haven't tested it and I have no clue if it will work for you or not..

It will need to be modified to suite your needs.

Code: Select all

<?php

if ((isset($_POST['submit'])) AND ($_POST['submit'] == 'Change The Sig'))
{
    foreach ($_POST as $key => $value) // loop through all the post variables
    {
        if (substr_count($key, '|') == 1) // check if it has a pipe in it..
        {
            // there is a pipe in it..
            list($RecordID, $FieldName) = explode('|', $key); // get the record number and the field name

            // insert this into an array..
            $NewValues[$RecordID][$FieldName] = $value;

        }

    }

    // now insert the new values into the database..
    foreach ($NewValues as $RecordID => $Record)
    {

        $Query = "UPDATE `TABLENAME` SET "; // start building the query

        foreach ($Record as $FieldName => $value)
        {
            $Query .= "`" . mysql_real_escape_string($FieldName) . "`='" . mysql_real_escape_string($value) . "', ";

        }

        $Query = substr($Query, 0, -2);

        $Query .= " WHERE `ID`='" . mysql_real_escape_string($RecordID) . "' LIMIT 1";
        
        $result = mysql_query($Query); // execute the query..

    }
}



$value = "5";
$db = mysql_connect("", "", "");
mysql_select_db("bhdsig",$db);
$result = mysql_query("SELECT * FROM pid ORDER BY pid", $db)or die("Error: ".mysql_error());

echo "<FORM NAME='Name_01' ACTION='options.php' METHOD='POST'>";

while ($row = mysql_fetch_assoc($result))
{
        $pid = $row['pid'];
        echo
        "<input type='hidden' NAME='" . $row['ID'] . "|S1' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S2' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S3' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S4' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S5' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S6' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S7' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S8' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|S9' VALUE='". $value ."'>
        <input type='hidden' NAME='" . $row['ID'] . "|Background' VALUE='Afghanistan'>
        <input type='hidden' NAME='" . $row['ID'] . "|TextColor' VALUE='Black'>
        <input type='hidden' NAME='" . $row['ID'] . "|FontStyle' VALUE='Arial'>
        <input type='hidden' name='" . $row['ID'] . "|oncethrough' value='Yes'>
        <input type='hidden' name='" . $row['ID'] . "|playerid' value='". $pid ."'>";

}

echo "<input class='black' type='submit' name='submit' value='Change The Sig'>";
echo "</FORM>";
?>