If / else statement inside echo needed

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tasteofchaos
    New Member
    • Dec 2009
    • 4

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

    Code:
    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>";
    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
    Last edited by Atli; Dec 13 '09, 07:24 PM. Reason: Removed link due to heavy advertising.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Have you tried doing something like this?
    [code=php]
    $text = ''; // Something from your database
    $maxChars = 78; // Or whatever.

    if(strlen($text ) > $maxChars) {
    $text = substr($text, 0, $maxChars - 3);
    $text .= "...";
    }

    echo "Something with the '$text' in it.";[/code]

    Comment

    • tasteofchaos
      New Member
      • Dec 2009
      • 4

      #3
      Hey,

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

      Code:
      $text = '$row[album]'; // Something from your database
      $maxChars = 10; // Or whatever.
      
      if(strlen($text) > $maxChars) {
          $text = substr($row[album], 0, $maxChars - 3);
          $text .= "...";
      }
      
      
      
      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.

      Comment

      • Atli
        Recognized Expert Expert
        • Nov 2006
        • 5062

        #4
        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.
        [code=php]<?php
        $maxChars = 10;
        if(strlen($$row['album']) > $maxChars) {
        $row['album'] = substr($row['album'], 0, $maxChars - 3);
        $row['album'] .= "...";
        }
        echo "...{$row['album']}...";
        ?>[/code]
        [code=php]<?php
        $maxChars = 10;
        $substitute = $row['album'];
        if(strlen($subs titute) > $maxChars) {
        $substitute = substr($substit ute, 0, $maxChars - 3);
        $substitute .= "...";
        }
        echo "...{$substitut e}...";
        ?>[/code]

        Comment

        • tasteofchaos
          New Member
          • Dec 2009
          • 4

          #5
          Thanks, this worked lovely!

          Comment

          • kovik
            Recognized Expert Top Contributor
            • Jun 2007
            • 1044

            #6
            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.

            Code:
            echo "..." . ($condition ? "true" : "false") . "...";

            Comment

            Working...