Search and Sort with MySQL Data in PHP - php

I'm learning PHP, I create a page to show my data table in MySQL. I want to paging, searching, sorting with that data.
I write some conditions to search and sort. But it's not working. My Paging seem work fine.
<?php
echo "<form action = 'Activiti_Data_Table_Detail.php' method = 'post'>";
echo "<input type = 'text' name = 'valueToSearch' placeholder = 'Value to search'><br><br>";
echo "<input type = 'submit' name = 'search' value = 'Filter'><br><br>";
echo "<input type = 'submit' name = 'ASC' value = 'Ascending'><br><br>";
echo "<input type = 'submit' name = 'DESC' value = 'Descending'><br><br>";
echo "<table id = 'datatable' border='1'>";
echo "<thead>";
echo "<tr>";
echo "<th>ID_</th>";
echo "<th>PROC_INST_ID_</th>";
echo "<th>BUSINESS_KEY_</th>";
echo "<th>PROC_DEF_ID_</th>";
echo "<th>START_TIME_</th>";
echo "<th>END_TIME_</th>";
echo "<th>DURATION_</th>";
echo "<th>START_USER_ID_</th>";
echo "<th>START_ACT_ID_</th>";
echo "<th>END_ACT_ID_</th>";
echo "<th>SUPER_PROCESS_INSTANCE_ID_</th>";
echo "<th>DELETE_REASON_</th>";
echo "<th>TENANT_ID_</th>";
echo "<th>NAME_</th>";
echo "</tr>";
echo "</thead>";
//connect to database
$con=mysqli_connect("localhost","root","123456","activiti") or die(mysqli_connect_errno());
//define how many results per page
$result_per_page = 10;
//number of results stored in database
$sql = "SELECT * FROM act_hi_procinst";
$result = mysqli_query($con, $sql);
$num_of_results = mysqli_num_rows($result); //225
//determine number of total pages available
$number_of_pages = ceil($num_of_results/$result_per_page);
//determine current page is on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
//determine the SQL LIMIT starting number for the result on the displaying page
$this_page_first_result = ($page - 1) * $result_per_page;
if(isset($_POST['search']) & isset($_POST['ASC'])){
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
ORDER BY END_TIME_ ASC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['search']) & isset($_POST['DESC'])) {
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
ORDER BY END_TIME_ DESC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['search'])) {
$valueToSearch = $_POST['valueToSearch'];
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%'
LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['ASC'])) {
$sql = "SELECT * FROM act_hi_procinst ORDER BY END_TIME_ ASC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} elseif (isset($_POST['DESC'])) {
$sql = "SELECT * FROM act_hi_procinst ORDER BY END_TIME_ DESC LIMIT" .$this_page_first_result . ',' . $result_per_page;
} else {
$sql = "SELECT * FROM act_hi_procinst LIMIT" .$this_page_first_result . ',' . $result_per_page;
}
$result = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($result)) {
echo "<tbody>";
echo "<tr>";
echo "<td><a href='Data_Table_Detail.php' target='_blank'>" . $row['ID_'] . "</a></td>";
echo "<td>" . $row['PROC_INST_ID_'] . "</td>";
echo "<td>" . $row['BUSINESS_KEY_'] . "</td>";
echo "<td>" . $row['PROC_DEF_ID_'] . "</td>";
echo "<td>" . $row['START_TIME_'] . "</td>";
echo "<td>" . $row['END_TIME_'] . "</td>";
echo "<td>" . $row['DURATION_'] . "</td>";
echo "<td>" . $row['START_USER_ID_'] . "</td>";
echo "<td>" . $row['START_ACT_ID_'] . "</td>";
echo "<td>" . $row['END_ACT_ID_'] . "</td>";
echo "<td>" . $row['SUPER_PROCESS_INSTANCE_ID_'] . "</td>";
echo "<td>" . $row['DELETE_REASON_'] . "</td>";
echo "<td>" . $row['TENANT_ID_'] . "</td>";
echo "<td>" . $row['NAME_'] . "</td>";
echo "</tr>";
}
echo "</tbody>";
echo "</table>";
echo "</form>";
//display the links to the page
for($page = 1; $page <= $number_of_pages; $page++) {
echo '' . $page . ' ';
}
mysqli_close($con);
?>
When I run this, it say: Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result.
Can you help me?
Thank you!

try change all your ifs for this:
$valueToSearch = isset($_POST['search']) ? $_POST['valueToSearch'] : '';
$orderBy = isset($_POST['ASC']) ? 'ORDER BY END_TIME_ ASC' : (isset($_POST['DESC']) ? 'ORDER BY END_TIME_ DESC' : '');
$sql = "SELECT * FROM act_hi_procinst WHERE concat(ID_, PROC_INST_ID_, PROC_DEF_ID_, START_TIME_, END_TIME_, DURATION_) LIKE '%".$valueToSearch."%' " . $orderBy . " LIMIT " . $this_page_first_result . ',' . $result_per_page;

Related

PHP Simple Pagination

the below code is getting some values from DB by "select option form" , i recently added Pagination snip to limit the results, when i run the code it fetch 5 recorders as defined,but didn't show the remaining number of pages.
what im doing wrong here ?
<?php
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_POST['form_val']) && isset($_POST['form_val'])) {
$_POST['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_POST['Location']) && isset($_POST['Location'])) {
$sql = $sql . " AND location =" . $_POST['Location'];
}
$strtdate = $_POST['Sday'];
$enddate = $_POST['Eday'];
if (!empty($_POST['Sday']) && isset($_POST['Sday']) && !empty($_POST['Eday']) && isset($_POST['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_POST['Sday']) && isset($_POST['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_POST['Eday']) && isset($_POST['Eday']))
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_POST['Site']) && isset($_POST['Site'])) {
$sql = $sql . " AND u.site=" . $_POST['Site'];
}
$sql = $sql . " LIMIT $start_from, $per_page";
if (mysqli_query($conn, $sql)) {
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$total_pages = ceil($rowcount / $per_page);
echo "<center><a href='?page=1'>" . 'First Page' . "</a> ";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "<a href='?page=$total_pages'>" . 'Last Page' . "</a></center> ";
} else {
echo '<p>No Results Found !!!</p>';
}
}
}
?>
As I said in my comments, for displaying pagination links:
You're counting total number of rows but incorporating LIMIT and OFFSET clauses in your SELECT query, this won't give the correct number of row count. Your SELECT query should not contain this part, ... LIMIT $start_from, $per_page.
Since you're filtering the results based on several $_POST data, you should incorporate those conditions in your pagination links as well, otherwise when you visit a different page(through pagination link), you won't get the desired result, and that's because $_POST data will not be retained when you hop from page to page. Better that you change the method of your <form> from POST to GET, because in this way it'd be easier for you to catch and manipulate things when you hop from one page to another using pagination links.
So based on the above points, your code should be like this:
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_GET['form_val']) && isset($_GET['form_val'])) {
$_GET['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_GET['Location']) && isset($_GET['Location'])) {
$sql = $sql . " AND location =" . $_GET['Location'];
}
$strtdate = $_GET['Sday'];
$enddate = $_GET['Eday'];
if (!empty($_GET['Sday']) && isset($_GET['Sday']) && !empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_GET['Sday']) && isset($_GET['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_GET['Site']) && isset($_GET['Site'])) {
$sql = $sql . " AND u.site=" . $_GET['Site'];
}
$data_query = $sql . " LIMIT $start_from, $per_page";
$result = mysqli_query($conn, $data_query);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$query_result = mysqli_query($conn, $sql);
$total_rows = mysqli_num_rows($query_result);
$total_pages = ceil($total_rows / $per_page);
parse_str($_SERVER["QUERY_STRING"], $url_array);
unset($url_array['page']);
$url = http_build_query($url_array);
?>
<center>First Page
<?php
for ($i = 1; $i <= $total_pages; $i++) {
?>
<?php echo $i; ?>
<?php
}
?>
Last Page</center>
<?php
} else {
echo '<p>No Results Found !!!</p>';
}
}

PHP - Table id the same

How can I put identical columns (sorted by "level" in this case) together? I am making a highscore where I list them by level from my database. I want them to have the same ID if they are the same level.
But I do not want to display the ID on the others. Only the first one.
Here is an example:
ID - Name - Level
1 - John - 5
2 - David - 4
3 - Josh - 3
- Sam - 3
4 - George - 2
So I want to put them together, but if they have the same level, only the first one displays the ID.
I don't want it to look like:
1 - John - 5
2 - David - 4
3 - Josh - 3
3 - Sam - 3
4 - George - 2
Right now, it is just listing everyone, and giving each one a unique ID. Even if they have the same "level".
How can I fix this? Here is my code:
<?php
$sql = mysql_query("SELECT * FROM rookstayers ORDER BY level DESC LIMIT 0, 500");
$id = 1;
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$level = $row['level'];
$world = $row['world'];
$account = $row['accountstatus'];
$status = $row['onlinestatus'];
$onrow = '';
$typeServ = '';
$player_name = urlencode($name);
if ($status == 1){
$status = 'Online';
$onrow = 'online';
} else {
$status = 'Offline';
$onrow = 'offline';
}
if ($account == 'Premium Account'){
$account = 'Premium';
} else {
$account = 'Free';
}
if ($world == 'Aurora' || $world == 'Aurera'){
$typeServ = 'active';
} else {
$typeServ = '';
}
echo "<tr class=" . $typeServ . ">";
echo "<td>" . $id . "</td>";
echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
echo "<td>" . $level . "</td>";
echo "<td>" . $world . "</td>";
echo "<td>" . $account . "</td>";
echo "<td class=" . $onrow . ">" . $status . "</td>";
echo "</tr>";
$id++;
}
echo "</tbody>";
echo "</table>";
?>
you can create a temporary variable for the current level and check it to see if the id will be shown on the output or not.
$id = 1;
$last_player_lvl = '';
while($row = mysql_fetch_array($sql)){
///....
echo "<tr class=" . $typeServ . ">";
echo "<td>" . ( ($last_player_lvl == $row['level']) ? '' : $id ) . "</td>";
echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
echo "<td>" . $level . "</td>";
echo "<td>" . $world . "</td>";
echo "<td>" . $account . "</td>";
echo "<td class=" . $onrow . ">" . $status . "</td>";
echo "</tr>";
if($last_player_lvl == $row['level']){
$id = $id;
}else{
$id++;
}
$last_player_lvl = $row['level'];
//....

mysql insert record from existing table with insert button

Sorry if my english is not correct. I will try my best..
The question is how can I insert record from existing table with insert button. Everything work fine except variable $book. It records value 0 to database. Here is code..
//if the user press "INSERT BOOK" button there will be new record in database
<?php
$reader=$_SESSION['user_id'];
$book=$row['book_id'];
if (isset($_POST['update'])){
$sql = "INSERT INTO `read` (`read_id`, `book`, `reader`) VALUES (LAST_INSERT_ID(), '$book' '$reader')";
$result = mysql_query($sql) or die(mysql_error());
if ($result) {
echo " Yes, it works!";
}
else{
echo "noup!";
}
}
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
if(preg_match("/^[ a-zA-Z]+/", $_POST['name'])){
$name=$_POST['name'];
$result = mysql_query("SELECT * FROM book b JOIN `read` r ON (b.book_id = r.book) JOIN users u ON (r.reader = u.user_id) WHERE b.book_name LIKE '%" . $name . "%' or b.writer LIKE '%" . $name . "%' AND u.user_id !=". $_SESSION['user_id']);
$num_rows = mysql_num_rows($result);
if($num_rows>=1){
echo "<table id='table1'>
<tr>
<th>book_id</th>
<th>BOOKNAME</th>
<th>WRITER</th>
<th>PAGES</th>
</tr>";
while($row=mysql_fetch_array($result)){
echo "<form action=add_book.php method='post'>";
echo "<tr>";
echo "<td>" . $row['book_id'] . "</td>";
echo "<td>" . $row['book_name'] . "</td>";
echo "<td>" . $row['writer'] . "</td>";
echo "<td>" . $row['pages'] . "</td>";
//echo "<td>" . "<input type = 'hidden' name = 'hidden' value =" . $row['book_id'] . " </td>";
echo "<td>" . "<input class = 'field' type = 'submit' name = 'update' value = 'INSERT BOOK'>" . " </td>";
echo "</tr>";
echo "</form>";
}//WHILE LOOP
echo "</table>";
}
else{
echo "NO RESULTS";
}//else
}//if(preg_match..)
} //if(isset($_GET['go'])){
} //if(isset($_POST['submit'])){
?>

How to insert different selected value from drop down list into database

Scenario : The administrator is to assign different companies to different student.
Problem : All student is given the same company of the last student in the form.
How do i make my hidden input work so that the correct selected drop down values of companies for each estudent to reflect correctly on the database?
$result = mysqli_query($con,"SELECT student_id, admin_no, name, GPA, gender FROM student_details WHERE jobscope1= 'Information Technology' ORDER BY `GPA` DESC; ");
$result2 = mysqli_query($con,"SELECT job_title FROM job_details WHERE jobscope='Information Technology' ORDER BY `job_title` ASC;");
/*options sections start*/
$options= '';
while ($row2 = mysqli_fetch_assoc($result2))
{
$options .='<option value="'. $row2['job_title'] .'"> '. $row2['job_title'] .'</option>';
}
/*options sections end*/
//return the array and loop through each row
while($row = mysqli_fetch_assoc($result))
{
$adminno = $row['admin_no'];
$name = $row['name'];
$gpa = $row['GPA'];
$gender = $row['gender'];
echo "<tr>";
echo '<input type=hidden name=admin_no value='. $adminno . '/>';
echo "<td>" . $adminno . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $gpa . "</td>";
echo "<td>" . $gender . "</td>";
echo "<td><select name='ddl' { myform.submit('') }'>".$options."</select></td>";
}
echo "</tr>";
The php form action :
$query = mysqli_query($con, "SELECT * FROM student_details WHERE jobscope1 = 'Information Technology';");
while ($row = mysqli_fetch_assoc($query))
$complist = $_POST['ddl'];
$result4 = mysqli_query($con, "UPDATE `student_details` SET `company`= '" . $complist . "' WHERE `jobscope1` = 'Information Technology';");
Try this:
//return the array and loop through each row
while($row = mysqli_fetch_assoc($result))
{
$adminno = $row['admin_no'];
$name = $row['name'];
$gpa = $row['GPA'];
$gender = $row['gender'];
echo "<tr>";
//changed here (this is called an input array which makes it hold multiple
//values with same name)
echo "<input type='hidden' name='admin_no[]' value='". $adminno ."'/>"; //edited
echo "<td>" . $adminno . "</td>";
echo "<td>" . $name . "</td>";
echo "<td>" . $gpa . "</td>";
echo "<td>" . $gender . "</td>";
//changed here too
echo "<td><select name='ddl[]' >".$options."</select></td>"; //edited
}
In your PHP:
if(isset($_POST['ddl'])){
foreach( $_POST['ddl'] as $index => $val ) //edited extra { here
{
$result4 = mysqli_query($con, "UPDATE `student_details`
SET `company`= '" . $val . "'
WHERE `jobscope1` = 'Information Technology'
AND `admin_no` = '".$_POST['admin_no'][$index]."';");
}
}
In this all your values will be updated (whether you changed any dropdown or not). If you want to limit only the changed dropdowns to be updated then you can have a hidden input variable which changes its value on change of dropdown and update your query only if the hidden input matches.

printing [mysql_query] result in a table

I want to print mysql_query result in a table. I know how to do it but I am just confused. I tried this.
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$i = 1;
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($i<=2 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=4 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=6 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
while($i<=8 && $row = mysql_fetch_array($sql)){
echo "<td>" . $row[id] . " : " . $row[name] . "</td>";
++$i;
}
echo "</tr><tr>";
echo "</tr></table>";
}
?>
As you can see it is written again and again with a slight change of 2,4,6,8 in the while loop. It works but the problem is I cant rewrite it again and again as when the website will go live it will have more than 1000 entries. Could You guys help me out by suggesting another way to do this?
""** I need it to be like these dots (dots represent records in the database) **"""
. . . .
. . . .
. . . .
THANKS in Advance.
Ramzy
<?php
mysql_connect("localhost","root","") or die("Could not Connect.");
mysql_select_db("check") or die("Could not Select DB");
$table = "cc";
$query = "select * from $table";
$sql = mysql_query($query);
if($sql){
echo "<table border='5'><tr>";
while($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
echo "</tr></table>";
}
?>
while($row = mysql_fetch_array($sql)) {
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
I don't really see what's the problem here.
By the way you should never call you're array like this $row[id] but you should quote the key instead $row['id']; Because if a constant id exists it will screw up your code and also for performance reason.
Just use
$limit = 1000;//place any value you need here to limit the number of rows displayed
while ($i<=$limit && $row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
++$i;
}
Also, that limit is unnecessary if all you want is to flush every record to the output. You could just do
while ($row = mysql_fetch_array($sql)){
echo "<td>" . $row['id'] . " : " . $row['name'] . "</td>";
}
And it will stop as soon as there are no more records.
To print all database rows into an HTML-table, use:
echo '<table>';
$i = 0; // $i is just for numbering the output, not really useful
while($row = mysql_fetch_array($sql))
{
echo '<tr><td>' . $i . ' - ' . $row['id'] . ' : ' . $row['name'] . '</td></tr>';
$i++;
}
echo '</tr></table>';
here is a general function I use:
function query_result_to_html_table($res, $table_id = NULL, $table_class = NULL, $display_null = true)
{
$table = array();
while ($tmp = mysql_fetch_assoc($res))
array_push($table, $tmp);
if (!count($table))
return false;
echo "<table cellpadding=\"0\" cellspacing=\"0\" border=\"0\" "
. ($table_id ? "id=\"$table_id\" " : "")
. ($table_class ? "class=\"$table_class\" " : "") . ">";
echo "<tr>";
foreach (array_keys($table[0]) as $field_name) {
echo "<th>$field_name";
}
foreach ($table as $row) {
echo "<tr>";
foreach ($row as $col => $value) {
echo "<td>";
if ($value === NULL)
echo "NULL";
else
echo $value;
}
echo "\n";
}
echo "</table>\n";
return true;
}
I Got The Answer.. I wanted it to be like this. I made this and It Actually Works.
<?php
$i = 1;
mysql_connect("localhost" , "root" , "") or die('Could not Connect.');
mysql_select_db("db") or die('Could not select DB.');
$query = "select * from `check`";
$sql = mysql_query($query) or die(mysql_error());
echo "<table border='5' width='50%'><tr><th>Name</th><th>Gender</th></tr></table><table border='5' width='50%'><tr>";
if($i<3){
echo "<td align='center'>".$row['name']."</td>";
echo "<td align='center'>".$row['gender']."</td>";
++$i;
} else {
echo "<td align='center'>".$row['name']."</td><td align='center'>".$row['gender']."</td>";
echo "</tr>";
$i = 1;
echo "<tr>";
}
}
echo "</table>";
?>
</div>
Thank You Guys For Your Support

Categories