0

I would like to know how to insert this object method:

$object = new User;
echo $object->getAllUsers();

within this PHP/HTML attribute of "src":

echo "<a href='#'>
      <img id='#' src='#howtoinsert?'></a>;

...because that object method contains the data $uid from my database that I want to echo out. I've tried many ways but they will not work. How would I do this?

This is the "Class User" that contains that object method:

class User extends Dbh {
    public function getAllUsers() {
        $stmt = $this->connect()->query("SELECT * FROM indeximg");
        while ($row = $stmt->fetch()) {
            $uid = $row['username'];
            return $uid;    
        }
    }
}
codo7081
  • 25
  • 7
  • Possible duplicate of [php - insert a variable in an echo string](https://stackoverflow.com/questions/8054989/php-insert-a-variable-in-an-echo-string) – Mike Sep 26 '18 at 00:39
  • @Mike That page didn't have the answer to my question and I tried those examples on the page given, but they did not work for me :( – codo7081 Sep 26 '18 at 00:47
  • 1
    Show us the code then. – Mike Sep 26 '18 at 00:52
  • Possible duplicate of [PHP Array: Contents from result set](https://stackoverflow.com/questions/10506103/php-array-contents-from-result-set) – gview Sep 26 '18 at 01:05
  • What's the point in looping over your query results when you `return` on the first iteration? – Phil Sep 26 '18 at 01:33
  • @Phil I'm not sure, I followed this code from a tutorial. It does work when retrieving data but I'm just having trouble with it for this one issue. – codo7081 Sep 26 '18 at 01:43

1 Answers1

1

Here is the code that could build your markup

$object = new User();
$users = $object->getAllUsers();

foreach($users as $currUser)
{
    echo '<a href="#"><img id="#" src="'.$currUser.'.jpg"></a>';
}

But it looks like your User class needs some help as well. As is, it will only return the first user. Try something like this:

class User extends Dbh
{
    public function getAllUsers()
    {
        $stmt    = $this->connect()->query("SELECT * FROM indeximg");
        $output = [];
        while ($row = $stmt->fetch())
        {
            $uid      = $row['username'];
            $output[] = $uid;
        }

        return $output;
    }
}
Rob Ruchte
  • 3,569
  • 1
  • 16
  • 18
  • I greatly appreciate your help but I tried this along with changing the user class, and it doesn't seem to work for me. I'll try to look up some tutorials on how to resolve this. Thank you again – codo7081 Sep 26 '18 at 01:17
  • For some reason the image keeps showing as broken, I'll need to write up sql code to insert an actual image then retry your code again. I inserted dummy values into my database instead of using an html form. Likely a fault on my end – codo7081 Sep 26 '18 at 01:39
  • I want to sincerely apologize for my last comment. Your method is the correct way to echo out the object. I didn't realize echo statements could be transferred to variables, my concatenation was also all wrong. Thank you very much for taking the time to thoroughly answer my question and I also appreciate your checking and fixing of my User Class. My mistake was in my database - I tried retrieving text as an image, didn't realize I couldn't do that, which is why the code didn't work - a definite fault on my end. But thank you very much for your help, it is greatly appreciated. – codo7081 Sep 26 '18 at 20:54