PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
In a bit of my code, I take out everything in a the title column from my DB and stick it into an array....but i was to have it so that after it displays 32 characters in and title it puts "..."
$listquery = "select title, num, time, user from poems where type='o' order by num $sortdir";
$listresult = mysql_query($listquery); // Connects to another table without a count
while ($row = mysql_fetch_assoc($listresult)) {
$titlesї] = $rowї"title"]; //get the title of each poem in a loop
if(strlen($titlesї]) > 35)
{
$titlesї] = substr($titlesї], 0, 32)."...";
}
} //end of while
So obviously there is something wrong there, and I'm horrible with array...can anyone help? Thanks!
You might want to consider separating these two processes. In one block of code, retrieve all of the values and populate the $titles[] array. Then in a separate function, try this:
function showShortDescription($title) { // pass in a title to process
$str_len = strlen($title);
$end_point = $str_len-1;
$show_periods = false;
if ($str_len > 35) {
$end_point = 35;
$show_periods = true;
}
for ($x=0; $x<$end_point; $x++)
echo $titleї$x];
if ($show_periods === true)
echo "...";
}
$listquery = "select title, num, time, user from poems where type='o' order by num $sortdir";
$listresult = mysql_query($listquery);
while ($row = mysql_fetch_assoc($listresult)) {
$titlesї] = $rowї"title"]; //get the title of each poem in a loop
}
}
foreach($titles as $key => $value){
if(strlen($value) > 35)){
$titlesї$key] = substr($value, 0, 32)."...";
}
$listquery = "select title, num, time, user from poems where type='o' order by num $sortdir";
$listresult = mysql_query($listquery);
while ($row = mysql_fetch_assoc($listresult)) {
if(strlen($rowї"title"]) > 35)
{
$titlesї] = substr($rowї"title"], 0, 32)."...";
}
else
{
$titlesї] = $rowї"title"];
}
}