tags:

views:

386

answers:

5

Hello,

I just had some basic php questions to further my understanding as I learn, that I could not find easy answers to

  1. I have a php ajax application that generates a table of mysql rows. I would like to know if there is a way to get php to generate neat html, as it seems neat enough as I echo it out, but when "viewing source" the html is a huge jumbled block with no line breaks or anything. Is there a trick to doing this?

  2. What is the best way to limit table output for a mysql database, so only the first 10 records or so are displayed, and there are automatically generated next and previous links to go between records?

  3. When outputting information with php from a mysql database, what is the best way to handle booleans? What is the easiest way to display the words "yes" or "no" or a tick or a cross? edit: I do not mean should I use words or pictures, but rather how to show either in response to a boolean

+1  A: 
  1. You could use the PHP Tidy Addon. Keep in mind, that filtering the HTML with it uses quite some Server CPU. Alternatively you have to print the line breaks yourself (echo "\n";) However I suggest, that you leave it as it is. Just consider how many people will benefit from reading the source code vs. how many people will benefit from smaller file sizes if you don't format the HTML nicely.

  2. You have to do that manually. There are some helping libraries available on the net. Just Google it.

  3. That's up to you to decide and depends on the application. Keep your users needs in mind.

BlaM
+1  A: 
  1. There exists a very nice PHP HTML templating engine called Smarty.

  2. select * from table limit 10, 10; will show results between 10 and 20

  3. that's really your decision from a UI point of view, personally I would use True or False

adam
+2  A: 
  1. If you want to see your HTML output in a nice format, I'd recommend using Firebug. It puts all your HTML into a collapsible tree format and even lets you merely click on an item to show you where it is in the source.
  2. There's no nice and easy way to generate the pagination (showing only 10 results, and then giving you back/next page buttons) of a search result natively. I'm sure that some frameworks would help you out in that regard, but the simplest method is to use the LIMIT keyword in your SQL. eg: you want to see page 3? SELECT * FROM mytable LIMIT 20,10
  3. The easiest way to display booleans is like this: var_dump($myBool) which will display bool(true).
    Alternatively you could do: echo $myBool ? "True" : "False"; - replace "True" and "False" with whatever you feel is easiest to read.
    If screen real-estate is a concern, an accepted way to display a bool is with 1 or 0.
    echo $myBool ? 1 : 0;, or even shorter still: echo (int)$myBool;
nickf
+4  A: 

Question 1

You need to separate the PHP code and the HTML output. The easiest thing to do, as a newbie, is :

  • Fill a var named $tab with your entries.

  • Create a file called "my_tab_template.php" including your xHTML code, using very few PHP to unpack $tab and only with the PHP alternative syntax.


<table>
   <?php foreach ($tab as $line) : ?>
      <tr>
         <?php foreach ($line as $cell) : ?>
           <td><?php echo $cell ?></td>
         <?php endforeach; ?>
      </tr>
    <?PHP endforeach; ?>
</table>


  • Include my_tab_template.php right after you have filled $tab.

Don't worry about optimization and performances, this will certainly not be your website bottleneck as you starting to code and you will make some things far more troublesome ;-)

Then, in your next project, when you will feel fine doing this, try to learn about the MVC pattern (a little search on SO may help). Don't listen to people talking about templating systems, and else. Don't try to start running a 150 cc before passing your driving licence.

Question 2

This is not a PHP question. What you want to do is to limit the output from your database. You can do that using the "LIMIT" SQL keyword.

You can use :

  • LIMIT 10 : this will limit your query to the 10 first row (= LIMIT 0,10)

  • LIMIT X, Y : this will limit your query to Y rows, starting from the row X

Remember to sort your query result with "ORDER BY" before using LIMIT to avoid nasty surprises.

And there is no automatic pagination in PHP. There are some PHP libraries that does the dirty job for you, but before you use them, I recommand to hack your own solution first to understand the mechanism. It's just about checking vars and using "LIMIT", really.

Then you may have a look to PEAR where a standard way to do it exists. But don't try too hard to find it, you'd better code it yourself first.

Question 3

If your database store a boolean, therefore it will output "0" for false, anything else (most probably "1") for true. Just test it :

if ($my_bool) 
    echo "True";
else
   echo "False";

In PHP, there is a shortcut to say that, but you have no obligation to use it. Anyway, it's good to know it exists. Meet the boolean operator :

echo  $my_bool ? "True"  : "False" ;
e-satis
shouldn't that be echo $my_bool ? "True" : "False" ;
Tom Haigh
Oh yeah ! Python coder habit :-) (ternary ends up like this : "True" if my_bool else "False") I fix that, thanks.
e-satis
+1  A: 

Just touching on the first point since it looks like the other two have been answered well already:

If it already looks OK in the HTML and you're just worried that it's all jumbled when you view source, just add "\n" to the end of each line as you print it.

Sean