Skip to main content
Inspiring
January 8, 2014
Answered

Calling functions within an echo statement in PHP/Mysqli

  • January 8, 2014
  • 1 reply
  • 1483 views

I have a table written in php that is filled from a mysqli result set.

It draws the table, and fills it from the resultset array, like below using a correctly figured connection:

<?PHP /* DRAWN AS A TABLE */

echo "<table border=0>";

while( $row = mysqli_fetch_array( $resultset, MYSQLI_NUM))

/*$s = (date('d/m/Y', strtotime($row[1]))) - this is the conversion for reference */

echo "<tr><td>$row[0] : $row[4] : $row[2]</td>

<td rowspan=15>

<img name=\"picspot\" src=\"\"

width=\"300\" height=\"240\" alt=\"image \"

$row[10] >

</td>

  </tr>

<tr>

    <td>Pictured : $row[1] </td> /* <td>Pictured : $s </td> */

  </tr>

/* more rows go here */

</table>"

?>

This code works correctly and produces a table in the correct format and value with the data inside the table as shown:

$row[1] contains a time stamp in the format 2003-10-17 00:00:00 and prints this in the table next to the word "Pictured".

I need to convert this to a date using something like:

$s = (date('d/m/Y', strtotime($row[1])))

If I include this in the echo statement it just prints the function  above instead of the date.

If I do  the date conversion immediately after the while statement, and then include

<td>Pictured : $s </td>  instead of <td>Pictured : $row[1] </td>

that works fine, but it also kills all other output from the $resultset array.

I am obviously doing something wrong but cannot figure it out.

Short of calling the query twice and storing the first $row[1] in a variable, converting it to a date and printing it on a second query print run, which looks like overkill, I cannot find a solution.

Can you?

All suggestions welcome.

Howard Walker

This topic has been closed for replies.
Correct answer whatalotofrubbish

After a lot of searching, I had to resort to a second query as follows:

$getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT photodate FROM table WHERE ref = $t"));

$pdate = $getID['photodate'];

$pdate= (date('d/m/Y', strtotime($pdate)));

.

.

.

<td>Pictured : $pdate </td>

produced the correct output.

Moral - if you work at it long enough there is aways a way!

Have fun!

Howard Walker

1 reply

whatalotofrubbishAuthorCorrect answer
Inspiring
January 9, 2014

After a lot of searching, I had to resort to a second query as follows:

$getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT photodate FROM table WHERE ref = $t"));

$pdate = $getID['photodate'];

$pdate= (date('d/m/Y', strtotime($pdate)));

.

.

.

<td>Pictured : $pdate </td>

produced the correct output.

Moral - if you work at it long enough there is aways a way!

Have fun!

Howard Walker