nicedad Posted September 11, 2015 Share Posted September 11, 2015 Hi everyone, I'm trying to retrieve data from MySql table. It works greate unfortunately, I couldn't plot the table in a beautiful way (see attached image).As I'm not professional in PHP, I only try to generate code and it worked but this desigen problem.It need it to be like the table in the image 2. thanks in advance, Here is the code: <!doctype html public "-//w3c//dtd html 3.2//en"> <html> <head> <title>Test</title> <style> label,a { font-family : Arial, Helvetica, sans-serif; font-size : 12px; } </style> </head> <body> <?Php //ini_set('display_errors', true);//Set this display to display all erros while testing and developing the script error_reporting(0);// With this no error reporting will be there include "include/z_db.php"; $todo=$_POST['todo']; $search_text=$_POST['search_text']; if(strlen($serch_text) > 0){ if(!ctype_alnum($search_text)){ echo "Data Error"; exit; } } ////////// Displaying the search box ///// echo "<table> <tr><td colspan=2 align='center'>"; echo "<form method=post action=''><input type=hidden name=todo value=search> <input type=text name=search_text value='$search_text'><input type=submit value=Search><br> <input type=radio name=type value=any checked>Suche by Beruf <input type=radio name=type value=exact>Search by Gemeinde </form> "; echo "</td></tr>"; /////////// if form is submitted the data processing is done here/////////////// echo "<tr><td width='600' valign=top>"; if(isset($todo) and $todo=="search"){ $type=$_POST['type']; $search_text=ltrim($search_text); $search_text=rtrim($search_text); if($type<>"any"){ $query="select * from worker3 where Gemeinde='$search_text'"; }else{ $kt=split(" ",$search_text);//Breaking the string to array of words // Now let us generate the sql while(list($key,$val)=each($kt)){ if($val<>" " and strlen($val) > 0){$q .= " Beruf like '%$val%' or ";} }// end of while $q=substr($q,0,(strLen($q)-3)); // this will remove the last or from the string. $query="select * from worker3 where $q "; } // end of if else based on type value echo "<span style='background-color= #FFFF00'>Sie habe folgende Daten ausgewehlt</span><br>"; $count=$dbo->prepare($query); $count->execute(); $no=$count->rowCount(); if($no > 0 ){echo " No of records = ".$no."<br><br>"; echo "<table style='border: solid 1px blue; background: red'>"; echo "<tr><th>sid</th><th>sname</th><th>age</th><th>Sex</th></tr>"; foreach ($dbo->query($query) as $row){ echo "<table style='border: solid 1px blue; background: gray'>"; echo "<tr><td>$row[id]</td><td>$row[name]</td><td>$row[Geburtstag]</td><td>$row[Staatsangehörigkeit]</td><td>$row[Kontaktdetails]</td><td>$row[Gemeinde]</td><td>$row[Beruf]</td><td>$row[Erfahrung]</td></tr>"; } echo "</table>"; }else { echo " No records found "; } }// End if form submitted echo "</td><td width='400' valign=top>"; echo " Full records here "; $query="select * from worker3"; echo "<table style='border: solid 1px blue; background: red'>"; echo "<tr><th><ID</th><th>Name</th><th>Geburtstag</th><th>Staatsangehoerigkeit</th><th>Kontaktdetails</th><th>Gemeinde</th><th>Beruf</th><th>Erfahrung</th></tr>"; foreach ($dbo->query($query) as $row){ echo "<table style='border: solid 1px blue; background: gray'>"; echo "<tr><td>$row[id]</td><td>$row[name]</td><td>$row[Geburtstag]</td><td>$row[Staatsangehörigkeit]</td><td>$row[Kontaktdetails]</td><td>$row[Gemeinde]</td><td>$row[Beruf]</td><td>$row[Erfahrung]</td></tr>"; } echo "</table>"; echo "</td></tr></table>"; ?> <center> Return to <a href=search-keyword.php>Search Keyword</a> <br><br><a href='http://www.eanda-group.com'>Here we are</a></center> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/298131-cs-problem/ Share on other sites More sharing options...
Ch0cu3r Posted September 11, 2015 Share Posted September 11, 2015 Its is because you are outputting the <table> tag inside your foreach loops, this will mean it will create a new table for every result returned from your query. You do not want this to happen, all you want is the results to be output inside new table row( <tr></tr> tags). All you need to do is remove this line from inside your loops echo "<table style='border: solid 1px blue; background: gray'>"; Quote Link to comment https://forums.phpfreaks.com/topic/298131-cs-problem/#findComment-1520663 Share on other sites More sharing options...
nicedad Posted September 11, 2015 Author Share Posted September 11, 2015 Hi Ch0cu3r, thanks so much for the replay.however, the line in the loop is to separate the rows in the table. I tried your suggestion unfortunately still same problem (the only difference is the rows ware in one color).wat I wanted is, the title's row should corresponds the records rows in the space and there should be a enugh space between title's row as well as between records. cheers, Quote Link to comment https://forums.phpfreaks.com/topic/298131-cs-problem/#findComment-1520668 Share on other sites More sharing options...
Ch0cu3r Posted September 11, 2015 Share Posted September 11, 2015 No. As I said you do not want to be creating a new table for each row returned by your query. Otherwise the columns in the table will always be misaligned, as per image 1. If you want to have your table be like image 2, then you need apply padding to the <th> and <td> tags in your css. th { padding: 10px; background-color: red; } td padding: 5px; background-color: grey } Live HTML example of having a new table for every row compared and without a new table. Quote Link to comment https://forums.phpfreaks.com/topic/298131-cs-problem/#findComment-1520670 Share on other sites More sharing options...
nicedad Posted September 11, 2015 Author Share Posted September 11, 2015 Ch0cu3rthanks so much Ch0CU3R , i really appreciate your help. It did work perfectly. best regards, Quote Link to comment https://forums.phpfreaks.com/topic/298131-cs-problem/#findComment-1520675 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.