Jump to content


Photo

Formatting Boolean Output


  • Please log in to reply
5 replies to this topic

#1 liquidprozak

liquidprozak
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 October 2006 - 09:24 PM

I am having a heck of time getting my Admin portion of my site to list a record, which is a Boolean type, as  Yes or No, not as 1 or 0.  I am listing data from MySQL that shows all users who are registered, but under the Admin column, I would like it to say Yes or No, not 1 or 0.  Coldfusion has a nifty command called YesNoFormat(), but alas, it appears there is nothing remotely simple to do this in PHP.  Any help would be greatly appreciated.

Thank you

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 October 2006 - 09:47 PM

Without seeing you code, it's a little hard to tell you what you are doing wrong.

Try something like this:
<?php
$yn = 1;
echo ($yn)?'Yes':'No';
?>

Ken

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 26 October 2006 - 09:52 PM

(Ken beated me) if nothing else:

<?php
$blah = 0;

echo ($blah) ? "yes" : "no";

// or make a function
function YesNoFormat($x) {
   return ($x)? "yes" : "no";
}		

echo YesNoFormat($blah);
?> 

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 26 October 2006 - 10:02 PM

Or you can do it the sql code

SELECT CASE WHEN admin=1 THEN 'Yes' ELSE 'No' END AS admin
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 liquidprozak

liquidprozak
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 October 2006 - 10:12 PM

Nice....the data is being pulled from a MySQL database, so it looks to me like the SQL code would work for my needs.  I can't thank everyone enough.  I was ready to start pulling my hair out.

#6 liquidprozak

liquidprozak
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 26 October 2006 - 10:18 PM

Well, here is a piece from my code.

<div id="content"><!-- InstanceBeginEditable name="TopContent" -->
    <div class="feature">
      <h3>Below is a listing of all news records in the database.</h3>
      <p><a href="add.php">Add a new News Record</a></p>
      <p>&nbsp;  </p>
      <table border="1" cellpadding="1" cellspacing="1">
        <tr>
          <th>Date Added</th>
          <th>Header</th>
          <th>News</th>
          <th>Online</th>
        </tr>
        <?php do { ?>
          <tr>
            <td><a href="edit.php?NewsID=<?php echo $row_rsNews['NewsID']; ?>"><?php echo $row_rsNews['DateAdded']; ?></a></td>
            <td><?php echo $row_rsNews['Header']; ?></td>
            <td><?php echo $row_rsNews['News']; ?></td>
          <td><?php echo $row_rsNews['Online']; ?></td>        </tr> <-This is where I want to display Yes or no

          <?php } while ($row_rsNews = mysql_fetch_assoc($rsNews)); ?>
      </table>
    </div>




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users