Board index   FAQ   Search  
Register  Login
Board index php forum :: Database mySQL & php coding

'for'-loop problem

Codes here !

Moderators: macek, egami, gesf

'for'-loop problem

Postby beloven » Fri Dec 27, 2002 5:15 am

Hello,

This is a piece of php code but I have got a problem. The problem is about the input in the database. I have got 5 different values for Nummer. These are nummmer1 - nummer5
I thought I could resolve the problem with a 'for' loop. Bur this doesn't work. When I manually type for example Nummer= '$Nummer1', this works, but with my 'for'-loop is doenst give the data in the database but something like 'Nummer1'. It must read de proper value.
Anyone a suggestion?

Thx in advance.
P.

if ($submit) {
for($i = 1; $i <= $aantal; $i++){
$Nummervar = "Nummer" .$i;
$Artiestvar = "Artiest" .$i;
echo $Nummervar;
// is er geen Id dan updaten anders editeren
if ($id) {
$sql = "UPDATE cd SET Nummer='$Nummervar',Artiest='$Artiestvar',Cd='$Cd' WHERE id=$id";
} else {
$sql = "INSERT INTO cd (Nummer,Artiest,Cd) VALUES ('$Nummervar','$Artiestvar','$Cd')";
}
// run SQL against the DB
$result = mysql_query($sql);
}
}
beloven
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Dec 27, 2002 5:09 am

Postby Joan Garnet » Fri Dec 27, 2002 8:27 pm

Hi, beloven,
I don't understand your case too much...
What are you trying to do exactly??

If you do something like this -->
Code: Select all
$submit="ok";
$aantal=5;

if ($submit) {
   for($i = 1; $i <= $aantal; $i++){
      echo $Nummervar = "Nummer" .$i."<br>";
      echo $Artiestvar = "Artiest" .$i."<p>";
   }
}

You will see that it works just fine.
You get this -->
Nummer1
Artiest1
Nummer2
Artiest2

Nummer3
Artiest3

Nummer4
Artiest4

Nummer5
Artiest5


So, there's no problem with numbers.
The weird thing comes with the Mysql query.
I don't think you want to do what the script does.
If you have a look at the script you'll see that what you are doing is update the same row time after time.

Maybe you want something like this -->
Code: Select all
if ($submit) {
   for($i = 1; $i <= $aantal; $i++){
      $Nummervar[$i] = "Nummer" .$i;
      $Artiestvar[$i] = "Artiest" .$i;
      if ($id) {
         $sql = "UPDATE cd SET Nummer='$Nummervar[$i]',Artiest='$Artiestvar[$i]',Cd='$Cd' WHERE id='$id'";
      } else {
         $sql = "INSERT INTO cd (Nummer,Artiest,Cd) VALUES ('$Nummervar[$i]','$Artiestvar[$i]','$Cd')";
      }
   }
}

Well,
I don't have enough data to guess what those vars do, or where they come from.

I hope it helped
;)
User avatar
Joan Garnet
Moderator
Moderator
 
Posts: 387
Joined: Sat Aug 03, 2002 2:56 am
Location: Mars

Postby beloven » Sat Dec 28, 2002 1:45 am

ok,

At first I wanna thank you for your reply. Unfortunately it doesn't work either. But i'm going to try to explain it better.

The problem is that someone gets a javascript prompt which ask for how many songtitles are on 1 cd. Then it generates a form with x-times inputypes. This form is submitted and the name attribute of the 'nummer' & 'artiest' varies. This is the code :

Code: Select all
<form method="post" action="insert.php" name="form1">
   <input type="Hidden" name="form_var">

  <?php
  if ($id) {
    // editing so select a record
    $sql = "SELECT * FROM cd WHERE id=$id";
    $result = mysql_query($sql);
    $myrow = mysql_fetch_array($result);
    $id = $myrow["id"];
    $first = $myrow["Nummer"];
    $last = $myrow["Artiest"];
    $address = $myrow["Cd"];
    // print the id for editing
    ?>
    <input type=hidden name="id" value="<?php echo $id ?>">
    <?php
  }
  ?>
      <center>
      <?php
         echo ("
   <table  border=\"1\" width=\"100%\">
      <tr>
         <td colspan='3'>
            <table border='0' bordercolor=\"#666666\" width='100%'>
               <tr>
                  <td align=\"center\" width='100' bgcolor=\"#666666\">
                     <img src=\"images/logo.gif\" border=\"0\">
                  </td>
                  <td bgcolor=\"#666666\" valign='middle' align='center'>
                     <font color='white'>
                        <font size='+3'>
                           !! IMPULS' MEAN BIG INPUT MACHINE !!<br><hr></font>Om te stoppen 2x op annuleren klikken en dan het venster gewoon sluiten
                        </font>
                  </td>
                  <td bgcolor=\"#666666\" align=\"right\" width='100'>
                     <img src=\"images/logo.gif\" border=\"0\">
                  </td>
               </tr>
            </table>
         </td>
      </tr>
      </table><br>");
      ?>
         <script type="text/javascript">
            var nummers = prompt("Aantal nummers op de Cd","Geef het aantal nummers op de Cd");
            var cdnaam = prompt ("Naam van de Cd","Geef de naam van de Cd");
            document.form1.form_var.value = nummers;
            document.write("<table border=\"0\">");
            for (i = 1; i <= nummers; i++){
               document.write("<tr><td><font color=\"white\" size=\"+2\">");
               document.write(i);
               document.write("</font></td>");
               document.write("<td><font color=\"white\">Nummer:</font></td><td><input type=\"Text\" name=\"Nummer");
               document.write(i);
               document.write("\"></td></tr><tr><td>&nbsp;</td><td><font color=\"white\">Artiest:</font></td><td><input type=\"Text\" name=\"Artiest");
               document.write(i);
               document.write("\"></td></tr><tr><td>&nbsp;</td></tr>")
            }
            document.write("<tr><td>&nbsp;</td><td><font color=\"white\">Titel cd :</font></td><td>");
            document.write("<input type=\"Text\" name=\"Cd\" value=\"");
            document.write(cdnaam);
            document.write("\">");
            document.write("</td></tr>");
            document.write("<tr><td>&nbsp;</td></tr><tr><td colspan=\"2\" align=\"center\"><input type=\"Submit\" name=\"submit\" value=\"Voeg nummer in\"></td></tr>");
            document.write("</font></table>");
         </script>
      </center>            
  </form>


After the submit it must get entered in the database. But when I make the php loop It doesn't put the value in the database but for example just Nummer 1. I really don't know anymore what to try...

Anyway thanks for truying to help me.
Maybe to make everything easier here is the complete php script : http://users.pandora.be/jhimpuls/insert.php
Code: Select all
Last edited by beloven on Sat Dec 28, 2002 5:38 am, edited 1 time in total.
beloven
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Dec 27, 2002 5:09 am

Postby beloven » Sat Dec 28, 2002 5:38 am

Hello,

Thanks but i have found the solution myself.

Code: Select all
$Nummervar[$i] = ${"Nummer" .$i};


I put the ${} around it so php recongizes it as a variable and not as a string.

Thanks
P.
beloven
New php-forum User
New php-forum User
 
Posts: 3
Joined: Fri Dec 27, 2002 5:09 am


Return to mySQL & php coding

Who is online

Users browsing this forum: No registered users and 4 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.