Jump to content

Dynamic select statement


nac20

Recommended Posts

Hi,

 

I have a page where the user selects a number from a dropdown (1-6), depending on the number chosen the next page displays a form with 1-6 blocks of data with repeated instances of the same fields (e.g. firstname_1, lastname_2, firstname_2, lastname_2 etc).

 

When this data is posted to the subsequent page I extract the post so the contents are in variables with the same names.

I know which number the user selected as I pass this through as a hidden variable.

 

What I want to do is perform a dynamic select statement so that the values clause can concatenate 'lastname_' + number but I read that this wasn't possible.

 

I then tried to prepare the values variable before the select statement using a php reference

 

e.g. $lastname = '&$lastname_'.$i where $i is the loop instance

 

but I get the name of the variable not the value in $lastname.

 

Any ideas please as I'm struggling and have had to hard code an if block?

 

I also have the same problem with Javascript validation.

 

Thanks in advance,

Alan

 

 

Link to comment
https://forums.phpfreaks.com/topic/116937-dynamic-select-statement/
Share on other sites

You should get the idea of what I'm trying to do from this:

 

extract($_POST);

 

// the above extract could result in the variables $hid_firstname_0 / $hid_lastname_0 as a minimum up to $hid_firstname_5 / $hid_lastname_5 depending on the number selected by the user (1-6), this value will be in $numLetters

 

for ($i = 0; $i < $numLetters; $i++) {

 

  $hid_firstname = '&$hid_firstname_'.$i;

  $hid_lastname = '&$hid_lastname_'.$i;

 

  $sql = "INSERT INTO tbl_letter(lt_firstname, lt_lastname)

          VALUES ('$hid_firstname', '$hid_lastname')";

 

}

 

// The above code is actually putting the string '&$hnid_firstname' into the table field rather than the value of this variable

You should get the idea of what I'm trying to do from this:

 

extract($_POST);

 

// the above extract could result in the variables $hid_firstname_0 / $hid_lastname_0 as a minimum up to $hid_firstname_5 / $hid_lastname_5 depending on the number selected by the user (1-6), this value will be in $numLetters

 

for ($i = 0; $i < $numLetters; $i++) {

 

  $hid_firstname = '&$hid_firstname_'.$i;

  $hid_lastname = '&$hid_lastname_'.$i;

 

  $sql = "INSERT INTO tbl_letter(lt_firstname, lt_lastname)

          VALUES ('$hid_firstname', '$hid_lastname')";

 

}

 

// The above code is actually putting the string '&$hnid_firstname' into the table field rather than the value of this variable

 

Try using this instead:

 

<?php

$hid_firstname = "&$hid_firstname_$i";
$hid_lastname = "&$hid_lastname_$i";

?>

 

should work.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.