What does this complicated Print() example do?

Discussion in 'PHP' started by ColorWP.com, Jan 4, 2010.

  1. #1
    print ($i?', ':'').str_replace('_', ' ', $r3['word']);
    PHP:
    This line is inside a While loop, so $i is the counter and $r3['word'] is an element from a mysql_fetch'd array, but what does this line of code do and how do I simplify it? All I want is to apply a ucfirst() function to the string before printing/echoing, but I can't seem get it to work currently.

    In case you are curious, here's a bigger piece of code surrounding this line:
    $q = mysql_query('SELECT synset_id FROM '.$sqlpre.'synset WHERE word="'.str_replace(' ', '_', $w).'" ORDER BY w_num ASC');
    $n = mysql_num_rows($q);
    
      while($r = mysql_fetch_array($q)) {
        if($n > 1)
          print '<li>';
        $q2 = mysql_query('SELECT gloss FROM '.$sqlpre.'gloss WHERE synset_id='.$r['synset_id']);
        $r2 = mysql_fetch_array($q2);
        print htmlspecialchars($r2['gloss']);
        $q2 = mysql_query('SELECT synset_id_2 FROM '.$sqlpre.'similar WHERE synset_id_1='.$r['synset_id']); 
        $n2 = mysql_num_rows($q2);
        if($n2) {
          print ' <em>';
          $i = 0;
          $syns = array();
          while($r2 = mysql_fetch_array($q2)) {
            $q3 = mysql_query('SELECT word FROM '.$sqlpre.'synset WHERE synset_id='.$r2['synset_id_2']);
            $r3 = mysql_fetch_array($q3);
            if(!in_array($r3['word'], $syns)) {
              print ($i?', ':'').str_replace('_', ' ', $r3['word']);
              array_push($syns, $r3['word']);
              $i++;
            }
          }
          print '</em>';
        }
    
        print '</li>'; 
        
      }
    PHP:

     
    ColorWP.com, Jan 4, 2010 IP
  2. marshall_26

    marshall_26 Peon

    Messages:
    82
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    print ($i?', ':'').str_replace('_', ' ', $r3['word']);
    PHP:
    basically if var $i is more than 0 add a comma and a space ', ' else nothing

    then append the term

    str_replace('_', ' ', $r3['word']);
    PHP:
    so if the term is "term_one" and it's on the second term which is "another_term" the end result would be

    term one, another term

    I am guessing you want

    Term one, Another term ?

    print ($i?', ':'').ucfirst(str_replace('_', ' ', $r3['word']));
    PHP:
    or use ucwords for

    Term One, Another Term

    print ($i?', ':'').ucwords(str_replace('_', ' ', $r3['word']));
    PHP:
    I bet you used

    print ucfirst(($i?', ':'').str_replace('_', ' ', $r3['word']));
    PHP:
    this would be incorrect ;)

    simplified the original code "no capitalization" would look something like

    
    $newWord = str_replace('_', ' ', $r3['word']);
    if($i>0) print ', ';
    print $newWord;
    
    PHP:
     
    Last edited: Jan 4, 2010
    marshall_26, Jan 4, 2010 IP
    www.Andro.ws likes this.
  3. ColorWP.com

    ColorWP.com Notable Member

    Messages:
    3,120
    Likes Received:
    100
    Best Answers:
    1
    Trophy Points:
    270
    #3
    Thanks for the information. It still looks so much complicated, but I think I got it.

    So tha's basically just 2 print()'s on one line. Like:
    echo $variable.$another;
    PHP:
     
    ColorWP.com, Jan 7, 2010 IP