473,398 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

If / else statement inside echo needed

Hey guys.

Unfortunately I can't break out of an echo to run the if statement I want to run because it's pulling data for a certain row.

I'd like text truncated to a certain amount of characters or a certain width but if the textual content is less than that amount of characters I want it untouched.

My code so far is...

Expand|Select|Wrap|Line Numbers
  1. 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>";
  2.  
And it generates this output (the right hand table of newest items in the album bit):

<Link removed>

I'm a bit of a novice, can anyone offer any suggestions?

Much appreciated
Dec 12 '09 #1
5 3906
Atli
5,058 Expert 4TB
Hey.

Have you tried doing something like this?
Expand|Select|Wrap|Line Numbers
  1. $text = ''; // Something from your database
  2. $maxChars = 78; // Or whatever.
  3.  
  4. if(strlen($text) > $maxChars) {
  5.     $text = substr($text, 0, $maxChars - 3);
  6.     $text .= "...";
  7. }
  8.  
  9. echo "Something with the '$text' in it.";
Dec 13 '09 #2
Hey,

I tried it but it hasn't worked. I don't know if I'm doing it right:

Expand|Select|Wrap|Line Numbers
  1. $text = '$row[album]'; // Something from your database
  2. $maxChars = 10; // Or whatever.
  3.  
  4. if(strlen($text) > $maxChars) {
  5.     $text = substr($row[album], 0, $maxChars - 3);
  6.     $text .= "...";
  7. }
  8.  
  9.  
  10.  
  11. 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'>".$row[album]."</a></td></tr>";
I have to use .$row[album]. in the echo bit to pull the album for that certain row.

Any ideas? it's not truncating at all.
Dec 13 '09 #3
Atli
5,058 Expert 4TB
The `$row['album']` variable doesn't "pull" data for that row. It is the data for that row. The data has already been pulled from the result set into the $row array, which you can use in whatever way you want.

So you can edit the `$row['album']` if you want, or use that to create a replacement variable to use in your echo statement.

You can use either of these methods, or mix them together in whatever way you want. You just need to make sure to echo the variable that is set to the return value of the `substr()` function, because that is what is actually truncating the string.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $maxChars = 10;
  3. if(strlen($$row['album']) > $maxChars) {
  4.     $row['album'] = substr($row['album'], 0, $maxChars - 3);
  5.     $row['album'] .= "...";
  6. }
  7. echo "...{$row['album']}...";
  8. ?>
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $maxChars = 10;
  3. $substitute = $row['album'];
  4. if(strlen($substitute) > $maxChars) {
  5.     $substitute  = substr($substitute, 0, $maxChars - 3);
  6.     $substitute .= "...";
  7. }
  8. echo "...{$substitute}...";
  9. ?>
Dec 13 '09 #4
Thanks, this worked lovely!
Dec 13 '09 #5
kovik
1,044 Expert 1GB
You could also make use of the ternary operator. It's like a function that returns one thing if the condition is true, or another thing if it is false.

Expand|Select|Wrap|Line Numbers
  1. echo "..." . ($condition ? "true" : "false") . "...";
Dec 14 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ciprian Ilie | last post by:
Hi there, If you look at the code below, you will see that I am using a template in order to display some photos on my website. I also have "previous" and "next" buttons/link which increment the...
5
by: Joe Blow | last post by:
I'm sure I'm missing something blindingly obvious here. I am relatively new to PHP, so excuse dumb mistakes. I a trying to check a form submitted from an earlier page, and check $_POST for empty...
11
by: dmbkiwi | last post by:
I am new to this group, and relatively new to python programming, however, have encountered a problem I just cannot solve through reading the documentation, and searching this group on google. I...
23
by: Invalid User | last post by:
While trying to print a none empty list, I accidentaly put an "else" statement with a "for" instead of "if". Here is what I had: if ( len(mylist)> 0) : for x,y in mylist: print x,y else:...
27
by: Ron Adam | last post by:
There seems to be a fair amount of discussion concerning flow control enhancements lately. with, do and dowhile, case, etc... So here's my flow control suggestion. ;-) It occurred to me (a...
8
by: Jim Michaels | last post by:
C:\prj\quiz\withusers>php tareports.php PHP Parse error: syntax error, unexpected T_ELSE in C:\prj\quiz\withusers\tareports.php on line 205 this is the section of code. if (isset($row4)) {...
2
by: naughtybynature | last post by:
<html> <head> <title>Search Questions</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php $query = '';
1
by: naughtybynature | last post by:
<html> <head> <title>Search Questions</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <?php $query = '';
2
by: pradeep.thekkottil | last post by:
I'm setting up an auction website using PHP and MySQL. There in the section where logged in members can put up new auction in a form, I want to run a form validation where I used if else statements...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.