Page 1 of 1

Combining variables

Posted: Thu Nov 21, 2002 11:15 am
by Rincewind
Hi there, I'm pretty new at this but I'm trying......
I'm trying to make a script that gets a list of merchandice and prices from a MySQL-server, prints them on screen in a form, lets the user edit what he wants and updates the database.

The get, print and edit-parts are working, my problem is this: I can't know how many items the list will hold (and the user can allso add some) so the update-script for the database must handle a dynamic amount of variables from the form.
The form, by means of a while-loop sends the data as name1, price1, name2, price2 etc...
How can I get the update-script to read these?
I tried to combine $name$ant ($ant is the counter in the while-loop in the update-script) but that didn't work.... :(

Tips, tricks, hints...Anyone??

Hope somebody can help.

Rincewind the Wizzard ;)

Posted: Thu Nov 21, 2002 11:19 am
by mydimension
instead of naming your form elements name1 and price1, use name[] and price[]. this sets up an array for use like this: $_POST['name'][1] and $_POST['price'][2]. then you can loop through the arrays until the end.

Thank you!

Posted: Thu Nov 21, 2002 11:48 am
by Rincewind
Hadn't thought I would get an answer this fast :)

Thanx a lot!

Rincewind

Hmmm, still can't get it to work

Posted: Thu Nov 21, 2002 5:18 pm
by Rincewind
...probably because I'm doing something horribly wrong, so here's the code:

Code: Select all

<?php
function liste($func)
{
  if ( ! $func )
    $func = "Øl";
  global $host, $user, $pass, $db, $pluss;
  $link = mysql_pconnect( $host, $user, $pass );
  if ( ! $link )
    die( "Couldn't connect to database-server" );
  mysql_select_db( $db, $link )
    or die( "Couldn't open database");
  $prisliste = mysql_query("SELECT * FROM $func");
  $ant1 = mysql_num_rows($prisliste);
  $ant = $ant1 + $pluss;
  $row = 0;
  $rowp = 1000;
  $rown="rown0";
  $rowp="rowp0";
  print" <form action='put.php?func=$func&ant=$ant' method='POST'>";
  while ( $row < $ant )
    {
      if ($row < $ant1)
	{
	  $navn = mysql_result($prisliste, $row, navn);
	  $pris = mysql_result($prisliste, $row, pris);
	  print"<input type='text' name='nameї]' size='40' value='$navn'>";
	  print"<input type='text' name='priceї]' size='10' value='$pris'><br>";
	}
      if ($row < $ant AND $row >= $ant1)
	{
	  print"<input type='text' name='nameї]' size='40'>";          //this is for adding lines

	  print"<input type='text' name='priceї]' size='10'><br>";
	  
	}
      ++$row;
    }
  $pluss=$ant-$ant1;
  ++$pluss;
  print"<br>";
  print"<input type='submit' value='Oppdater'>";
  print"</form>";
  mysql_close( $link );  
}
?>
That's the form posting the input.
And here's the script trying to put it into the database:

Code: Select all

<?php
$link = mysql_pconnect( $host, $user, $pass );
if ( ! $link )
     die( "Couldn't connect to database-server" );
 mysql_select_db( $db, $link )
     or die( "Couldn't connect to database" );
     
$delete = "DELETE FROM $func";
     if ( ! mysql_query( $delete, $link))
     {
       $dberror = mysql_error();
       print"$dberror";
     }
$row=0;
while($row < $ant)
{
  $name="$_POSTїname]ї$row]";
  $price="$_POSTїprice]ї$row]";
  print"$name";
  print"<BR>$price";
  $query = "INSERT INTO $func ( navn, pris ) values( '$name', '$price')";
  if ( ! mysql_query( $query, $link ))
  {
    $dberror = mysql_error();
    print"$dberror";
  }
  ++$row;
}
?>
?>
So if you could please look at it and tell me why I get

Array[0] 0
Array[1] 0
Array[2] 0
Array[3] 0
Array[4] 0

As the result posted in my database I would appreciate it a LOT!

Thanx

Rincewind

BTW, register_globas is on, in case that has anything to do with it....

Posted: Thu Nov 21, 2002 5:35 pm
by mydimension
modify these two lines as shwn:

Code: Select all

<?php
$name=$_POSTї'name']ї$row];
$price=$_POSTї'price']ї$row];
?>

YES!

Posted: Thu Nov 21, 2002 6:02 pm
by Rincewind
Thanx a bunch, I've been trying for hours.
Great help!

Rincewind :)