0

My objective is to extract each value of 3 columns of a SQL table. Although, I want to extract these values from different tables deppending on the value of listtype that is chosen. The columns Name and Reg_Date have the same name for all the tables. Although, the name of the column corresponding to ID may be either ID_Doctor, ID_Nurse or ID_Patient. How can I append $POST['listtype'] to $ID and be able to use it inside an echo? Below is the code that I tried, although the echo always returns the string ID_Doctor, ID_Nurse, or ID_Patient, deppending on the value of $_POST['listtype'].

$ID = '$ID_' .$_POST['listtype'];

while($count < $pages_c)
{
  while ($rows = mysql_fetch_array($results)) 
  { 
    extract($rows); 
    $numrows++;
    echo "<tr><td>$ID</td><td>$Name</td><td>$Reg_Date</td></tr>";
  }
$count++;
}
0

2 Answers 2

4

You can use variable variables to accomplish that.

echo "<tr><td>${$ID}</td><td>$Name</td><td>$Reg_Date</td></tr>";

Keep in mind that you do not need to use extract to get the values from your $rows array. You could read them directly using $rows['key'] syntax.

while ($rows = mysql_fetch_array($results)) 
{ 
    $numrows++;
    echo "<tr><td>{$rows[$ID]}</td><td>{$rows['Name']}</td><td>{$rows['Reg_Date']}</td></tr>";
}

Side note: mysql_* functions are deprecated as of PHP 5.5 and will be removed in the future. I'd recommend you to stick with MySQLi or PDO instead.

Sign up to request clarification or add additional context in comments.

Comments

0
echo "<tr><td>",$rows[$ID],"</td><td>$Name</td><td>$Reg_Date</td></tr>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.