0

I'm using a while loop and mysql_fetch_array to display info on a page from a database. How can I give a variable name to certain radio button elements that are echoed on the page, and then use those variable names for the radio buttons as $_POST variables when the form is submitted?

Here is the code:

session_start();
include 'acdb.php';

$prodid = $_SESSION['prodid'];

$e = "SELECT * FROM Images WHERE product_id=$prodid AND active='yes'";
$e_result = mysql_query($e,$connection) or die ('Could not get the list of all Images for this Product and Service');
$amount = mysql_num_rows($e_result);

while ($e_data = mysql_fetch_array($e_result)) {
    echo "<img src='images/thumbnails/" .$e_data[1]. "' border='0'>";
    echo "<label class='sel-photo'>DISPLAY PICTURE:</label> <input type='radio' name='{radio" .$e_data[0]. "}' value='1'>";
    echo "<br>";
    echo "<label class='sel-photo'>DO NOT DISPLAY PICTURE:</label> <input type='radio' name='{radio" .$e_data[0]. "}' value='0' checked>";
}                   
Marcel Gwerder
  • 8,353
  • 5
  • 35
  • 60
DanAdams
  • 165
  • 1
  • 3
  • 8
  • 2
    I appreciate your effort in writing this but I think you would get more response if you whould keep it more compact and try to concentrate on the actual question. It's just to much text. – Marcel Gwerder Apr 27 '14 at 15:03
  • What @MarcelGwerder says is indeed correct. People around don't want to bother reading stories. Just put some relevant part of code and brief info. Describe your problem as short as possible. Btw, it's not bad idea to provide some info about database structure as sample + please dont use `mysql_*` functions with database, use `PDO` or `mysqli_*`. – Wh1T3h4Ck5 Apr 27 '14 at 15:06
  • You set your name for that input by `...name='" .$query_result[0]. "'...`. What `$query_result[0]` returns is name you're looking for. So your job is just to check `$_POST` array on submit. – Wh1T3h4Ck5 Apr 27 '14 at 15:08
  • Thank you all for helping explain the benefits of shorter questions. I went ahead and edited my question down, so hopefully it will be more succinct and easier to understand what I am trying to accomplish. Also, thanks @Wh1T3h4Ck5 for explain about the mysqli thing. I will make that change in my code. – DanAdams Apr 27 '14 at 15:12
  • If you notice in the new example, I am trying to use concatenation in the name of the radio button so that each group of radio buttons echoed each have a unique way to be retrieved when the form is submitted. That is where I am running into my problem. – DanAdams Apr 27 '14 at 15:14

1 Answers1

1

You will get the result you are after by doing this

 echo "<label class='sel-photo'>DISPLAY PICTURE:</label> <input type='radio' name='radio" .$e_data[0]. "' value='1'>";

So assuming $e_data[0] = 'xxx' you would get html of

name='radioxxx'

PS. Its best, especially when using SELECT * to use the form $e_data['fieldname']. This is because the columns will be returned in the order they were created on the table when using *. If you/someone else decides to reorganise the table columns at a later date, they will be returned in a different order and $e_data[0] may now point to a different column.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Thanks for the reply @RiggsFolly. If I utilize the naming system that you mentioned, when I submit the form, how do I call the $_POST variable because I will not know if the value of $e_data[0] = 'xxx' or 'yyy'? For instance...if I was using a name for the radio button of name="radio", than my post variable would be $_POST['radio']. So what would the post variable be with a concatenated radio button name? – DanAdams Apr 27 '14 at 16:20
  • And thank you @RiggsFolly for explaining the SELECT options. That makes lots of sense and I think I will begin to implement that into my code. Much appreciated. – DanAdams Apr 27 '14 at 16:38