Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Calling functions within an echo statement in PHP/Mysqli

Engaged ,
Jan 08, 2014 Jan 08, 2014

Copy link to clipboard

Copied

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:

table1.JPG

$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.

table2.JPG

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

TOPICS
Server side applications

Views

1.4K
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Jan 09, 2014 Jan 09, 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!

table3.JPG

Have fun!

Howard Walker

Votes

Translate
Engaged ,
Jan 09, 2014 Jan 09, 2014

Copy link to clipboard

Copied

LATEST

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!

table3.JPG

Have fun!

Howard Walker

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines