Jump to content

If / else statement inside echo needed


aksdesigns

Recommended Posts

Hey guys,

 

I need to truncate some text so it always stays on one line. I need it to truncate after a certain amount of characters or width. So, if the text is long, I want it truncated but if it's not long - I want it left alone.

 

Unfortunately, I can't break out of an echo to write an if statement as I want to truncate text pulled for a certain row.

 

I have some code at the moment that doesn't work very well. Here it is:

 

echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', substr($row[album], 0, 30))."...</a></td></tr>";

 

It returns this result (on the right hand table showing the newest items - the album part):

 

http://www.megalyrics.net/index.html

 

I'm a PHP novice, could anyone offer an suggestions?

 

Thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/184909-if-else-statement-inside-echo-needed/
Share on other sites

I do have something there but it truncates whatever is there and seems to be working oddly (smaller album names are truncated really small and longer ones are truncating how i want them to)

 

I need something there to tell it to not touch ones that don't need truncating.

You might try something like this, though I haven't tested it:

 


// Check length of $row['album'] and create var $albumName based on it
if (strlen($row['album']) >= 30) {
    $albumName = substr($row['album'],0,30) . '...' ;
} else {
    $albumName = $row['album'] ;
}

// Echo out table using previously created $albumName
echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName."...</a></td></tr>";

 

I get an error saying:

 

Parse error: syntax error, unexpected ';' in /home/highr7/public_html/megalyrics/index.html on line 75

 

which is the last bit of this (what i used of your code)...

 

// Check length of $row['album'] and create var $albumName based on it
		if (strlen($row['album']) >= 30) {
		    $albumName = substr($row['album'],0,30) . '...' ;
		} else {
		    $albumName = $row['album'] ;
		}

		// Echo out table using previously created $albumName
echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName."...</a></td></tr>";

 

 

I've attached the file if that helps (without the code you sent and as php because it's usually html but won't allow me to upload that)...

 

 

 

[attachment deleted by admin]

try this:

 

// Check length of $row['album'] and create var $albumName based on it
         if (strlen($row['album']) >= 30) {
             $albumName = substr($row['album'],0,30) . '...' ;
         } else {
             $albumName = $row['album'] ;
         }

         // Echo out table using previously created $albumName
echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName)."...</a></td></tr>";

 

all i did was add a ) after $albumName because you werent closing the preg_replace function

It now doesn't error but even when I change a value to 200 or more, it still looks the same as it first did. Here's the code:

 

// Check length of $row['album'] and create var $albumName based on it
         if (strlen($row['album']) >= 200) {
             $albumName = substr($row['album'],0,30) . '...' ;
         } else {
             $albumName = $row['album'] ;
         }

         // Echo out table using previously created $albumName
echo "</td><td class='listingtitle'>Artist:</td><td><a href='http://megalyrics.net/search.html?c=".$row[artist]."' title='View ".$row[artist]." Lyrics'>".$row[artist]."</a></td></tr><td class='listingtitle'>Album:</td><td><a href='http://megalyrics.net/search.html?c=".$row[album]."' title='View ".$row[album]." Lyrics'>".preg_replace('/\s+?(\S+)?$/', '', $albumName)."...</a></td></tr>";

 

And the link showing that it still shows the same:

http://www.megalyrics.net

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.