Some basic PHP questions

I just had some basic php questions to make me realize I can't find the answers to

  • I have a php ajax application that generates a mysql row table. I would like to know if there is a way to get php to generate neat html as it seems neat enough when I repeat it, but when "view source" html is a huge jumbled block with no line breaks or whatever. Is there a trick for this?

  • What is the best way to limit the table output for mysql database so only the first 10 records are displayed or so and the next and previous links are automatically generated to navigate between the records?

  • When dumping information from php from mysql database, what is the best way to handle booleans? What's the easiest way to display the words "yes" or "no" or tick or cross? edit: I don't mean if I should use words or images, but rather how to show either in response to a boolean

0


source to share


6 answers


Question 1

You need to separate PHP code and HTML output. The easiest way to do it as a beginner:

  • Complete var tab named $ with your entries.

  • Create a file named " my_tab_template.php

    " including your xHTML code, using very little PHP to unpack $ tab and only with PHP syntax .

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


  • Turn my_tab_template.php

    it on immediately after filling in the $ tab.

Don't worry about optimization and performance, it certainly won't be a bottleneck for your site when you start coding, and you will make some things a lot more annoying; -)

Then on your next project, when you're feeling good, try learning about the MVC pattern (a little search on SO might help). Don't listen to people talking about templating systems and more. Do not try to run 150 cc before transferring your driver's license.

Question 2

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

You can use:



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

  • LIMIT X, Y: This will limit your query to Y-lines starting at line X

Remember to ORDER BY the query result before using LIMIT to avoid any unpleasant surprises.

And there is no automatic pagination in PHP. There are several PHP libraries out there that do the dirty work for you, but before using them, I recommend hacking your own solution first to understand the mechanism. This is just checking the var and using "LIMIT", really.

Then you can take a look at PEAR, where there is a standard way to do this. But don't try too hard to find it, you must first code it first.

Question 3

If your database stores a boolean value, so it will print "0" for false, anything else (most likely "1") for true. Just test it:

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

      

PHP has a shortcut to say this, but you are not required to use it. In any case, it's good to know that it exists. Introduction to the logical operator:

echo  $my_bool ? "True"  : "False" ;

      

+3


source


  • If you want to see your HTML output in a good format, I would recommend using Firebug. It puts all your HTML in a collapsible tree format and even lets you just click an element to show you where it is in the source.
  • There is no nice and simple way to generate pagination (showing only 10 results and then giving you back / next page buttons) from search results. I'm sure some structures will help you in this regard, but the easiest way is to use the LIMIT keyword in your SQL. eg: do you want to see page 3?SELECT * FROM mytable LIMIT 20,10

  • The simplest way to display gates is like this: var_dump($myBool)

    which will display bool(true)

    .
    Alternatively, you can do: echo $myBool ? "True" : "False";

    - replace "True" and "False" with what you think is easiest to read.
    If screen real estate is a concern, the accepted way to display bool is 1 or 0.
    echo $myBool ? 1 : 0;

    or shorter still:echo (int)$myBool;



+1


source


replace var with $amir

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>

      

+1


source


  • You can use PHP Tidy Addon . Be aware that filtering HTML with it uses quite some server CPU. Alternatively, you need to print the line breaks yourself (echo "\ n";) However, I suggest that you leave it as it is. Just think about how many people would benefit from reading the source code, as well as how many people would benefit from smaller file sizes if you didn't format HTML.

  • You have to do it manually. There are some help libraries on the net. Just google it .

  • What you decide depends on the application. Remember the needs of your users.

0


source


  • There is a very nice PHP template for templates called Smarty.

  • select * from table limit 10, 10;

    will display results between 10 and 20

  • what is really your solution in terms of UI, personally I would use True or False

0


source


Just touching on the first point, as it looks like the other two already got the answer:

If it already looks OK in HTML, and you're just worried about it getting mixed up when viewing the source, just add "\ n" to the end of each line when printing. In the meantime, there is no need to worry about it.

0


source







All Articles