0

I am trying to add a variable inside a variable

How can I do it ?

Here is my code, in which I have to place $day in the place of Mon (line 9) so that it echo data from SQL database according to the day of week.

Here is my code

<?php
include_once("config.php");
date_default_timezone_set('Asia/Kolkata');
//$timestamp = time();
$day = "" . date("D");
$result = mysqli_query($mysqli, "SELECT * FROM Time_table WHERE regd='". $_SESSION['TextBox1'] ."'   ORDER BY id DESC");
        while($res = mysqli_fetch_array($result)) {
            echo "<tr>";
            echo "<td style='width: 30%'>".$res['Mon']."</td>";
            echo "<td style='width: 30%'>".$res['time']."</td>";
            echo "<td style='width: 40%'>".$res['room_no']."</td>";
            }
        ?>

so that the line 9 should look like

echo "<td style='width: 30%'>".$res['$day']."</td>";

but it is wrong, I am able to figure it out how to do.

6
  • could you elaborate? you want to add an item into $res or $result? Commented Jul 26, 2017 at 22:06
  • i want to make it like this but i dont know how echo "<td style='width: 30%'>".$res['Mon']."</td>"; to echo "<td style='width: 30%'>".$res['$day']."</td>"; Commented Jul 26, 2017 at 22:08
  • You mean $res[$day] ? I don't see how that could help though. Or do you have a column for every day in your table? Commented Jul 26, 2017 at 22:12
  • yes @Isac i know it is wrong but i want to know the correct usage Commented Jul 26, 2017 at 22:14
  • Then it would be better say what columns does your table have, and what exactly are you trying to do. Commented Jul 26, 2017 at 22:15

2 Answers 2

1

It should be

echo '<td style="width: 30%">'.$res[$day].'</td>';

When you are accessing array elements, simply mention the variable which you are using to store the index. No need of quotes there.

Pay attention to the use of quotes.

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

Comments

0

When using $res['$day'] PHP actually looks for the index called $day in your array, since your SQL does not have a column names $day you will get an error saying the requested index was not found.

So using $res[$day] will pass the value of $date in to the array and if there exists an element for the value you passed, it will return.

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.