Page 1 of 1

A little array problem

Posted: Sat Aug 17, 2002 11:31 am
by gotDNS
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 "..."

Code: Select all

$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!

later on, -Brian

Posted: Sat Aug 17, 2002 1:19 pm
by protokol
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:

Code: Select all

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&#1111;$x];
   
   if ($show_periods === true)
      echo "...";
&#125;

Posted: Sat Aug 17, 2002 4:52 pm
by hob_goblin
or if you dont like the method above, do this:

Code: Select all

$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)) &#123; 
   $titles&#1111;] = $row&#1111;"title"];    //get the title of each poem in a loop 
   &#125; 
&#125;
foreach($titles as $key => $value)&#123;
if(strlen($value) > 35))&#123;
$titles&#1111;$key] = substr($value, 0, 32)."...";
&#125;

Posted: Sat Aug 17, 2002 5:07 pm
by gotDNS
Thanks guys...and thanks for the "..." code in the first place hob_goblin.

I did:

Code: Select all

$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)) &#123;
	if(strlen($row&#1111;"title"]) > 35)
	&#123;
		$titles&#1111;] = substr($row&#1111;"title"], 0, 32)."...";
	&#125;
	else
	&#123;
		$titles&#1111;] = $row&#1111;"title"];
	&#125;
&#125;
Thanks!