0

I have been trying to do the following: I have stored subjects in database seperated by commas to make an array. I am then running a foreach loop to get the subjects once they have been exploded. The part where i am stuck is I am trying to create variables based on the subject names to then run a mysqli code that will derive information from another table in DB Based on the subject name that i should be setting.

I've tried using eval, which does not display anything when i run the code.

$hwvar = "";
$hwsubject = "";
foreach ($hwarray as $hwvar) {
    eval("$hwvar = $row".""."['$hwvar']");            
}
?>
    <td><?php echo $hwvar; ?></td>

so i need the loop to produce the following for each item is loops through ($sujectname is what is stored and derived from the database e.g homework1) $subjectname = $row['$subjectname'];

5
  • Show us the data you are starting with and what you have done to it. Please Commented Aug 21, 2019 at 10:15
  • 1
    I would suggest putting the <td><?php echo $hwvar; ?></td> inside the loop and forget about all this EVAL() nonsense. As you have it now, the output would only get the LAST data item in the array anyway Commented Aug 21, 2019 at 10:16
  • eval() is evil Commented Aug 21, 2019 at 10:20
  • 1
    you are doing something catastrophically wrong, from "stored subjects seperated by commas" to creating distinct variables. Tell us your initial goal (a real life task before any php and mysql stuff) and get the correct way for doing so Commented Aug 21, 2019 at 10:21
  • Data is stored in array as follows: homework1,homework2 . i used explode and extract that on foreach. now issue is i need to make it show the following: $homework1 = $row['homework1']; but for each result in the array and then be able to echo that into the table Commented Aug 21, 2019 at 10:48

1 Answer 1

1

It would be so much simpler and safer (eval() is a dangerous function in the wrong hands) to just output the <TD> inside the loop

foreach ($hwarray as $hwvar) {
    $dat = $row[$hwvar];
    echo "<td>$dat</td>";
}
?>

NOTE: eval() should be avoided at almost all costs, for all sorts of reasons.

The simplest to understand is the distinct possibility that creating variables in the global scope dynamically has the possibility of overwriting one, or more, of your existing variables, making debugging a complete nightmare.

UPDATE, after comments

I dont have your database so I mocked up a simple example of what I assume you are doing

// dummy database result containing 2 result rows
$rows = [['home'=>'100', 'home2'=> '200'],['home'=>'300', 'home2'=> '400']];

// yoour list of required columns
$hwarray = ['home','home2'];

// loop over the query resultset 
// your code might be a while loop over a fetch like
// while ($row = $result->fetch_assoc()){
// for example

foreach ($rows as $row){
    foreach ($hwarray as $hwvar) {
        echo "<td>$row[$hwvar]</td>";
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

Issue is, the value of $hwvar needs to be used to collect data from another table in a database. So $hwvar has the value of "homework1" and "homework2" in the array. I then need it to create this string $hwvar = $row['$hwvar']; to pull out the data in the database which has a column name of "homework1" and another column "homework2". and then display that data in a table
Ok, I didnt get that from the original Question. Does my amendment satisfy your requirement?
Its not pulling the data out of the database. This is the logic it should be doing: the array values are (homework1, homework2) I need to run the following code from this data: $homework1 = $row['homework1']; $homework1 = $row['homework2']; I need to do this in a array incase more items are added to the array
It's still not working. This whole code is running inside a while array already.
Then show us ALL the relevant code and then we can better help you

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.