Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Monday, May 2, 2016

Problem with table in php

Problem with table in php


I'm trying to create a table in php that would show the data on the mysql database based on the check box that is checked by the user. As you can see in this screen shot, it will have problems when you did not check on a checkbox before the one that will be the last: http://www.mypicx.com/04282010/1/

Here is my code:

if($_POST['general'] == 'ADDRESS'){  $result2 = mysql_query("SELECT * FROM student WHERE ADDRESS='$saddress'");   ?>        LASTNAME" ?>    FIRSTNAME" ?>      MIDNAME" ?>      ADDRESS" ?>      GENDER" ?>    RELIGION" ?>    BIRTHDAY" ?>    CONTACT" ?>  
IDNO YEAR SECTION

What can you recommend so that the output will not look like this when you one of the checkbox before a checkbox is not clicked: http://www.mypicx.com/04282010/2/ alt text

Answer by Vinze for Problem with table in php


instead of

           

you should do something like

".$row['GENDER']."" ?>  

So that the tags only appears if the "if" statement is true.

Answer by outis for Problem with table in php


You only print table header elements () if the corresponding $is*Field* variable is set, but you print all table cells, only testing whether or not to print the cell contents.

Instead of all that, loop over the fields to be printed out. No need to test each and every field.

Example form:

Student Information

$label) { ?>

Parent Information

$label) { ?>

Form handler:

 $label) { ?>          $label) { ?>            

The foreach ($results as $row) { needs to be rewritten as a while loop if you stick with the outdated mysql driver, but works with PDOStatement. Switching to PDO also makes it easier to injection vulnerabilities, as prepared statement parameters are invulnerable to them. You can also rewrite that SELECT * to only fetch the requested columns, reducing DB load.

$validFields = array('last' => 'Last Name', 'first' => 'First Name', 'stAddr' => 'Address', ...);  $fields = array_intersect($validFields, $_POST['show']);  

You could even make it self-configuring by constructing the $validFields array by inspecting the DB table(s), though this would incur an extra table query.

Answer by thebluefox for Problem with table in php


Yeah, do it the same way as you've done the th tags, with the if statement around the td tags, rather than inside them. Way you've done it now will always show 9 columns, no matter what check boxes are selected.

Answer by Ben Everard for Problem with table in php


Ok first thing's first, let's clean your code up, because it's so difficult to read in it's current format:

  
'.$row['LASTNAME'].''); } ?> '.$row['FIRSTNAME'].''); } ?> '.$row['MI'].''); } ?> '.$row['ADDRESS'].''); } ?> '.$row['GENDER'].''); } ?> '.$row['RELIGION'].''); }?> '.$row['BIRTHDAY'].''); }?> '.$row['S_CONTACTNUM'].''); }?>
IDNO YEAR SECTION LASTNAME FIRSTNAME MIDNAME ADDRESS GENDER RELIGION BIRTHDAY CONTACT

Your best bet would be to try putting this code in and telling us if this improves things?

EDIT

Ah, as the others have said your tags are sitting outside of your condition, still, the above code is much easier to read and will help future debugging :-)

Answer by Mikulas Dite for Problem with table in php


Your 're printing the cells in the iteration, but only it's content depends on the condition.

' . $row['S_CONTACTNUM'] . '' ?>  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.