Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Senior Coder
    Join Date
    Aug 2005
    Posts
    1,119
    Thanks
    2
    Thanked 1 Time in 1 Post

    adding whitespace to echo problem.

    adding spaces to an echo.

    This code, which im using works:

    PHP Code:
    <?php // connect to the mysql database server.
    mysql_connect ($dbhost$dbusername$dbuserpass);
    mysql_select_db($dbname) or die('Cannot select database');
    $query "SELECT DISTINCT category FROM tutorials";
    $result mysql_query($query) or die('Error, query failed');
    while (
    $row mysql_fetch_array($result)) 
    {
        echo 
    $row['category'];
    }
    ?>
    What i would like to do, would look more like this, HOWEVER, it doesn't work. What do i change to make it work.

    PHP Code:
    <?php // connect to the mysql database server.
    mysql_connect ($dbhost$dbusername$dbuserpass);
    mysql_select_db($dbname) or die('Cannot select database');
    $query "SELECT DISTINCT category FROM tutorials";
    $result mysql_query($query) or die('Error, query failed');
    while (
    $row mysql_fetch_array($result)) 
    {
        echo 
    $row['category']" | ";
    }
    ?>

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    You have to use the dot ".". In PHP it joins strings together (concatenation operator).
    PHP Code:
    print $string ' space ' $another_string
    BTW as echo is not a function it is better to use "," like this:
    PHP Code:
    echo $string' space '$another_string
    It only works with echo though.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    echo $row['category'] . ' | ';

    Is this what you mean? Trying to separate categories with a pipe? If so, there's a better way.

    PHP Code:
    <?php // connect to the mysql database server.
    mysql_connect ($dbhost$dbusername$dbuserpass);
    mysql_select_db($dbname) or die('Cannot select database');
    $query "SELECT DISTINCT category FROM tutorials";
    $result mysql_query($query) or die('Error, query failed');
    $categories = array()
    while (
    $row mysql_fetch_array($result)) 
    {
        
    $categories[] = $row['category'];
    }
    echo 
    implode(' | '$categories);
    ?>
    "$question = ( to() ) ? be() : ~be();"

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Quote Originally Posted by marek_mar
    BTW as echo is not a function it is better to use "," like this:
    I'm curious as to why it's 'better'?

  • #5
    Regular Coder ralph l mayo's Avatar
    Join Date
    Nov 2005
    Posts
    951
    Thanks
    1
    Thanked 31 Times in 29 Posts
    Quote Originally Posted by GJay
    I'm curious as to why it's 'better'?
    Yeah, I think this is backwards. Commas are argument delimiters and periods are concatenation operators, so it seems if echo isn't a function it would be more in keeping with its spirit to pass it a big chunk of data that's been stuck together externally rather than a bunch of discrete units.

    If you're worried about speed, which you shouldn't be anyway because your system is almost certainly not going to be bottlenecked by 'echo' overhead, someone who benchmarked it in the PHP manual says concatenation actually tests faster.

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,075
    Thanks
    11
    Thanked 98 Times in 96 Posts
    <edit>lol got the wrong end of the stick </edit>
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by GJay
    I'm curious as to why it's 'better'?
    Yes It's the other way around. I made sure of it now. I mixed it up becouse I never use echo.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •