4

What i want to do is to display some data from mysql database if its set by user and if it not set then i just want to display some placeholder or "dummy" data. For example if user create profile he will by default get some random or placeholder picture as profile picture and when he change it i want to display that picture from database. Same goes for lets say user description or 'about me' section.

This is what i got so far

<?php 
$getInfo = $con->prepare("SELECT * FROM user_info WHERE userid=:userid");
$getInfo->execute(array(':userid'=>$userID));
$data = $getInfo->fetch(PDO::FETCH_ASSOC);

$description = $data['description'];
?>

<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <h3 class="page-title">USER DESCRIPTION</h3>
      <?php if(!empty($description)) {
        echo '<p>' . $description . '</p>';
      } else { ?>
      <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur incidunt, at error. Provident veritatis ratione laborum modi laudantium, quidem inventore facilis at qui blanditiis, sunt tempore labore sit, eligendi libero?</p>
      <?php } ?>
    </div>
  </div>
</div>

So here i am using !empty($description) but i am not sure if this is right way to do this and if this is always going to work. I am new to mysql and php and this is just for learning puropses. And just to add $userID is session that exists if user is logged in.

Nenad Vracar
  • 118,580
  • 15
  • 151
  • 176

2 Answers2

2

That will work assuming $data['description'] is always defined. However, it is arguably better to separate your logic and presentation.

<?php 
$getInfo = $con->prepare("SELECT * FROM user_info WHERE userid=:userid");
$getInfo->execute(array(':userid'=>$userID));
$data = $getInfo->fetch(PDO::FETCH_ASSOC);

$description = !empty($data['description']) ? $data['description'] : 'Lorem ipsum dolor sit amet, consectetur adipisicing elit. Tenetur incidunt, at error. Provident veritatis ratione laborum modi laudantium, quidem inventore facilis at qui blanditiis, sunt tempore labore sit, eligendi libero?';
?>

<div class="container">
  <div class="row">
    <div class="col-sm-12">
      <h3 class="page-title">USER DESCRIPTION</h3>
        <p>
          <?php echo $description; ?>
        </p>
    </div>
  </div>
</div>
imkingdavid
  • 1,411
  • 13
  • 26
  • No problem. If this helped you please give it an upvote and set it as the accepted answer (unless someone else has anything better to say, of course). Thanks! – imkingdavid Oct 05 '15 at 20:10
  • @imkingdavid I just have a question (i'll delete this comment after that). Even though the place holder is quite a big text, changing the order so you don't have to use `!` is a good practice? So it doesn't look something like a double negative? – FirstOne Oct 05 '15 at 20:16
  • 1
    @FirstOne No need to delete the comment, it might help someone else to see it and the response. You can order the condition in whichever way seems most logical and easiest to read. A way to keep from having a bunch of placeholder text pushing the 'else' half of the condition out of view would be to define the placeholder text in a `$placeholder` variable above the condition, and then the order wouldn't make too much of a difference. – imkingdavid Oct 05 '15 at 20:19
  • That is even better :) – Nenad Vracar Oct 05 '15 at 20:22
  • You said that will work assuming $data['description'] is always defined, but is there any possibility that its not defined? I think it must be defined it can only be empty or not. – Nenad Vracar Oct 05 '15 at 20:28
  • Assuming the `user_info` table has a `description` field and is never changed to not have a `description` field, then yes it will always be defined. However, I've just always been taught it's better to be safe than sorry, so for me it's second nature to ensure array indexes are never used without first checking that they exist. – imkingdavid Oct 05 '15 at 20:30
  • @FirstOne empty() does isset() for you. You don't have to do both. – imkingdavid Oct 05 '15 at 20:30
  • @imkingdavid I'm sorry, can you elaborate? If you're already checking if it's not empty, why the need to check if the index exists? How can the column inexistence cause trouble? Unless you're talking about using it somewhere else... – FirstOne Oct 05 '15 at 20:38
  • 1
    Previously, you defined `$description = $data['description'];` and you were not checking the index's existence and value until further down in the template. My change checked both the existence of the index and the value at the same time, preventing any errors and handling the logic at the same time. – imkingdavid Oct 05 '15 at 20:40
1

You can also do it direct in MySQL

You get always a result. if the userid not found they return the 2 SELECT from the UNION

SELECT * FROM (
  SELECT * FROM j WHERE userid =99999
  UNION ALL
  SELECT 0,'NOOOOOOOOO'
  ) AS c ORDER BY userid DESC LIMIT 1;

Here the Table

MariaDB [tmp]> select * from j;
+--------+-------+
| userid | bame  |
+--------+-------+
|      0 | 222   |
|      1 | a     |
|     22 | b     |
|     47 | c     |
|    333 | d     |
|    334 | fff   |
|    335 | hhh   |
|   NULL | hallo |
+--------+-------+
8 rows in set (0.00 sec)

and 2 Samples

MariaDB [tmp]> SELECT * FROM (   SELECT * FROM j WHERE userid =1   UNION ALL   SELECT 0,'NOOOOOOOOO'   ) AS c ORDER BY userid DESC LIMIT 1;
+--------+------+
| userid | bame |
+--------+------+
|      1 | a    |
+--------+------+
1 row in set (0.00 sec)

MariaDB [tmp]> SELECT * FROM (   SELECT * FROM j WHERE userid =99999   UNION ALL   SELECT 0,'NOOOOOOOOO'   ) AS c ORDER BY userid DESC LIMIT 1;
+--------+------------+
| userid | bame       |
+--------+------------+
|      0 | NOOOOOOOOO |
+--------+------------+
1 row in set (0.00 sec)

MariaDB [tmp]>
Bernd Buffen
  • 14,525
  • 2
  • 24
  • 39