Some Basic PHP Questions
I just had some basic php questions to further my understanding as I learn, that I could not find easy answers to
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?
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?
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
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.
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.
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" ;
- 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.
- 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
- 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;
replace var $amir with your variable
<table> <?php foreach ($amir as $yasir) : ?> <tr> <?php foreach ($yasir as $shah) : ?> <td> <?php echo $shah?> </td> <?php endforeach; ?> </tr> <?PHP endforeach; ?> </table>
There exists a very nice PHP HTML templating engine called Smarty.
select * from table limit 10, 10; will show results between 10 and 20
that's really your decision from a UI point of view, personally I would use True or False
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.
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.
You have to do that manually. There are some helping libraries available on the net. Just Google it.
That's up to you to decide and depends on the application. Keep your users needs in mind.