Jump to content

Recommended Posts

Hello all,

 

I have created a script that queries my MySQL database and gets a certain row, then echoes out:

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

while($row = mysqli_fetch_array($result))
  {
   echo "<li><a href=\"#\">"; 
  echo $row['col_5'];
  echo "</li>";
  }

I would like to check to see if each of the row outputs has a string that is more than 13 characters, and if so, strip it down to 13 and add "..." to the end of the string.  I did some research and added a line of code, but it still outputs the long string.  Any ideas on where I went wrong?

if(!$result = $db->query($sql)){
    die('There was an error running the query [' . $db->error . ']');
}

while($row = mysqli_fetch_array($result))
  {
$row = (strlen($row) > 13) ? substr($row,0,10).'...' : $row;    // ADDED THIS LINE
   echo "<li><a href=\"#\">"; 
  echo $row['col_5'];
  echo "</li>";
  }
Link to comment
https://forums.phpfreaks.com/topic/287311-substr-and-strlen/
Share on other sites

  • Solution

$row is an array of all the columns for the current row iteration. So you are attempting to use strlen and substr on the entire array.

 

I *assume* given your echo, that you really want this:

 

$row['col_5'] = (strlen($row['col_5']) > 13) ? substr($row['col_5'],0,10).'...' : $row['col_5']; // ADDED THIS LINE

Link to comment
https://forums.phpfreaks.com/topic/287311-substr-and-strlen/#findComment-1474033
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.