Jump to content

foreach with pdo


Go to solution Solved by Psycho,

Recommended Posts

Hi

 

I'm having  a problem getting the user_id into a table td

 

This is my php

<?php
                            if (empty($results)) {  
                            } else {
                                foreach ($results as $row) {
                                    echo "<tr><td>";
                                    echo $row['user_nome'];
                                    echo "</td><td>";
                                    echo $row['user_email'];
                                    echo "</td><td>";
                                    echo $row['user_tel'];
                                    echo '<td><a data-toggle="modal" data-id="echo $row['user_id']" class="open-AddDialog btn btn-sm btn-danger center-block" href="#myModal">Validar</a></td>';
                                }
                            }
                            ?> 

Giving me error in this line

  echo '<td><a data-toggle="modal" data-id="echo $row['user_id']" class="open-AddDialog btn btn-sm btn-danger center-block" href="#myModal">Validar</a></td>';

unexpected echo

 

Link to comment
https://forums.phpfreaks.com/topic/298857-foreach-with-pdo/
Share on other sites

You have an 'echo' inside the quoted text. Couple that with the fact that you start the string with a single quote and then put an array with a single quoted index inside the string, the PHP parser is confused (as am I).

 

Also, why do you have this?

if (empty($results)) {
} else {

There is no purpose for the TRUE branch of that statement. Just use this

]if (!empty($results)) {

Although, if $results is just an empty array, you don't need the empty check at all - just run the foreach.

 

EDIT: You are also creating some invalid HTML code. There is no closing TD for one element - and I would expect a closing TR on each foreach as well.

Edited by Psycho
Link to comment
https://forums.phpfreaks.com/topic/298857-foreach-with-pdo/#findComment-1524463
Share on other sites

  • Solution

Give this a try

 

<?php
 
    if (!empty($results))
    {
        foreach ($results as $row) {
            echo "<tr>\n"
            echo "  <td>{$row['user_nome']}</td>\n";
            echo "  <td>{$row['user_email']}</td>\n";
            echo "  <td>{$row['user_tel']}</td>\n";
            echo "  <td><a data-toggle='modal' data-id='{$row['user_id']}' class='open-AddDialog btn btn-sm btn-danger center-block' href='#myModal'>Validar</a></td>\n";
            echo "</tr>\n"
        }
    }
 
?>
  • Like 1
Link to comment
https://forums.phpfreaks.com/topic/298857-foreach-with-pdo/#findComment-1524465
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.