A little array problem

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!

Moderator: General Moderators

Post Reply
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

A little array problem

Post 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
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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;
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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;
User avatar
gotDNS
Forum Contributor
Posts: 217
Joined: Tue May 07, 2002 5:53 pm
Location: West Chester, PA

Post 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!
Post Reply