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 10 of 10

Thread: help?

  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    help?

    I need some help getting this to work. I want php to echo a row in my database "only if" it has information. but if it doesnt have info i want it to display alternative data
    <img src="uploads/none.gif"/>
    but it seems not to work. it still displays data no matter what.


    help me please?


    PHP Code:
    <?php 

    $show 
    = ('');
    $a $row_Blog['title']

                                                     if (
    $a <> $show)
                         { 
                         echo (
    $row_Blog['avatar']); }
                         else { 
                         echo (
    "<img src='uploads/none.gif'/>"); 
                         
                         } 
    ?>

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    You should use mysql_num_rows(), and a loop to output the rows. Check out the examples for the mysql_fetch_assoc() function.

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually Im using that function.

    $row_blog['title'] is using that function.
    Last edited by 40esp; 08-12-2007 at 11:01 PM.

  • #4
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    If row[2] is empty "alternative data" will be displayed, if row[2] has data then Location: plus the row data will be displayed.


    PHP Code:
    if (strlen($row[2]) > 0) {
    echo 
    "Location: $row[2]";

    } else {
    echo 
    "alternative data";


    Leonard Whistler

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Len Whistler View Post
    If row[2] is empty "alternative data" will be displayed, if row[2] has data then Location: plus the row data will be displayed.


    PHP Code:
    if (strlen($row[2]) > 0) {
    echo 
    "Location: $row[2]";

    } else {
    echo 
    "alternative data";



    I've tried that and i got this error:
    Code:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/web1/web/home.php on line 116
    I changed $row[2] to $row_Blog['avatar']. was I supposed to do that? if not what is the variable $row have?

  • #6
    Senior Coder Len Whistler's Avatar
    Join Date
    Jul 2002
    Location
    Vancouver, BC Canada
    Posts
    1,323
    Thanks
    26
    Thanked 100 Times in 100 Posts
    Quote Originally Posted by 40esp View Post
    I've tried that and i got this error:
    [CODE]I changed $row[2] to $row_Blog['avatar']. was I supposed to do that? if not what is the variable $row have?
    $row[2] is the 3rd column of your database - 1st column is $row[0] - you can also use the name of the columns, such as:

    PHP Code:
    $row[avatar
    Leonard Whistler

  • #7
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Or use curly brace syntax
    PHP Code:
    echo {$row_Blog['avatar']}; 
    PHP Code:
    $row[avatar
    Wouldn't you need to have that in double quotes to access it that way? Else, it'll first be interpreted as a constant.
    PHP Code:
    "$row[avatar]"
    http://www.php.net/types.array
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.

  • #8
    New Coder
    Join Date
    May 2007
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    got it. =] thankss all.

  • #9
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    Quote Originally Posted by StupidRalph View Post
    Or use curly brace syntax
    PHP Code:
    echo {$row_Blog['avatar']}; 
    PHP Code:
    $row[avatar
    Wouldn't you need to have that in double quotes to access it that way? Else, it'll first be interpreted as a constant.
    PHP Code:
    "$row[avatar]"
    http://www.php.net/types.array
    No, it will be parsed as long as it's in double quotes. You only need the curly braces if you use single quotes in the array output statement.
    ie.:
    PHP Code:
    $arr = array('test' => 'text');
    echo 
    $arr['test'] . "  $arr[test]  {$arr['test']}";
    //text text text 

  • #10
    Senior Coder
    Join Date
    Mar 2003
    Location
    Atlanta
    Posts
    1,037
    Thanks
    14
    Thanked 30 Times in 28 Posts
    Gotcha.
    Most of my questions/posts are fairly straightforward and simple. I post long verbose messages in an attempt to be thorough.


  •  

    Posting Permissions

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