Turn Data into a yes or no

Discussion in 'PHP' started by johneva, Feb 12, 2010.

  1. #1
    Hi

    I got a database where one of the feilds are bool giving a result of 0 or 1. Whats the easiest way of turning this into a yes or no?

    I tried this but it dont seem to work though.

    
    <?php 
    //connect to mysql
    //change user and password to your mySQL name and password
    mysql_connect("localhost", "johnmev_user1", "Pass1");
    	
    //select which database you want to edit
    mysql_select_db("cp"); 
    
    //If cmd has not been initialized
    if(!isset($cmd)) 
    {
       //display all the cars
       $result = mysql_query("select * from data order by id"); 
       
       //run the while loop that grabs all the cars
       while($r=mysql_fetch_array($result)) 
       { 
          //grab the make, model,special and the ID
          $make=$r['make'];//take out the make     
          $model=$r['model'];//take out the model
          $special=$r['special']; //take out the special
          $id=$r['id'];//take out the id
          
          //Translate special bool into yes no
    
          if ( $special == "1" ) {
    	$spec=yes;
    } else {
            $spec=no;
    {
    
    
         
    	 //show the make and model a link in a list
          echo "<li>";
          echo "<a href='edit.php?cmd=edit&id=$id'>Edit - $make $model</a> - $spec";
          echo "</li>";
        }
    }
    ?>
    
    PHP:
     
    johneva, Feb 12, 2010 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I think you are missing an ending bracket after:

    $spec=no;

    If there's more to it than that include the error message you are getting or whatever information you have that leads you to believe its not working.
     
    plog, Feb 12, 2010 IP
    johneva likes this.
  3. ProtegeSales

    ProtegeSales Guest

    Messages:
    88
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well, first you need to change

       while($r=mysql_fetch_array($result)) 
    PHP:
    To

        while($r=mysql_fetch_assoc($result)) 
    PHP:
    Notice the difference in fetching the array and the assoc. The array, still works - but you $vars will be called like so. $var[0] $var[1] $var[2] etc. In this instance you're calling the associated name for it. So, it would be mysql_fetch_assoc (I.E. $var['special'])

    Also, characters need to be encapsed by quotations in variables. I.E. $var = yes; Needs to be $var = 'yes';

    Thanks, hope this guides you in the right direction.
     
    ProtegeSales, Feb 12, 2010 IP
    johneva likes this.
  4. johneva

    johneva Well-Known Member

    Messages:
    1,480
    Likes Received:
    46
    Best Answers:
    1
    Trophy Points:
    170
    #4
    Lol asif I put an open bracket instead of close, cheers.

    Think I need a break now am tired and making dumb mistakes. cheers agen mate.
     
    johneva, Feb 12, 2010 IP
  5. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #5
    $spec = ($special) ? 'yes' : 'no';
    PHP:
     
    SmallPotatoes, Feb 13, 2010 IP