how to display the data in a column with sql and php - php

i have table in sql like this :
----------------------------------
| id | name | time1 | time2 |
----------------------------------
| 1 | softball | 05.00 | 10.00 |
| 2 | softball | 10.00 | 11.00 |
| 3 | softball | 11.00 | 14.00 |
-----------------------------------
here is my code :
$query = "select * from schejule";
$sql = mysql_query($query);
echo "<table class='table table-striped table-advance table-hover'>";
while ($u = mysql_fetch_array($sql)) {
echo "<tr><td>$u[time1] - $u[time2]</td></tr>";
}
echo "</table>";
but if i create like that, will display it like this :
-----------------
| 05.00 - 10.00 |
-----------------
| 10.00 - 11.00 |
-----------------
| 11.00 - 14.00 |
-----------------
i want to display it with php like this :
--------------------------------------------------------
| days | 05.00 - 10.00 | 10.00 - 11.00 | 11.00 - 14.00 |
--------------------------------------------------------
| mo | | | |
--------------------------------------------------------
| tu | | | |
--------------------------------------------------------
| we | | | |
--------------------------------------------------------
| th | | | |
--------------------------------------------------------
| fr | | | |
--------------------------------------------------------
| sa | | | |
--------------------------------------------------------
| su | | | |
--------------------------------------------------------
How can i display like that if i use php. I only know how to make it in rows.
Thx..

Try to prepare each column of a row before printing it to output stream.
Sample code for the first row (not tested):
$query = "select * from schejule";
$sql = mysql_query($query);
echo "<table class='table table-striped table-advance table-hover'>";
$row = '<tr><td>days</td>';
while ($u = mysql_fetch_array($sql)) {
$row .= "<td>$u[time1] - $u[time2]</td>";
}
$row .= '</tr>';
echo $row;
echo "</table>";

Related

Fetching/Putting 2 variables in a mysqli_num_row and mysqli_fetch_array in PHP

Basically I want to do a search by category and title. My problem is that the two are located in separate tables. I was thinking of putting 2 variables in the mysqli_num_rows or mysqli_fetch_array but I don't think it's the right idea. The $search variable is working already but I don't know what I will do for $searchcat that is a different table.
<table border="1">
<tr>
<th>ID</th>
<th>Survey Title</th>
<th>Category</th>
</tr>
<?php
if (isset($_POST['submit']))
{
include 'testdb.php'; //connection is written in other page (db.php)
$var =$_POST['search'] ;
$searchtype = $_POST['searchtype'];
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
echo "<h3>Search results:</h3>";
while($show = mysqli_fetch_array($get_data))
{
$id = $show['survey_id'];
$title = $show['title'];
$category = $show['categoryname']; //
echo "<tr align='center'>";
echo "<td><font color='black'>" .$id. "</font></td>";
echo "<td><font color='black'>" .$title. "</font></td>";
echo "<td><font color='black'>" .$category. "</font></td>";
}
}
else{
echo "No Records found!";
}
}
?>
</table>
</body>
This is table category (categoryname is what I need)
+-------------+---------------+-------------+
| category_id | categoryname | datecreated |
| 1 | Philosophical | |
| 4 | Political | |
| 6 | Social | |
This is table survey (title is all I need)
| 1 | survey_id | title | description | duration | gender | age_group_from | age_group_to |
| 2 | 44 | game1 | description1 | 5 | male | 0 | 18 |
| 3 | 45 | game2 | description2 | 25 | female | 18 | 25 |
| 4 | 46 | game3 | description3 | 89 | female | 26 | 35 |
This is table survey_header (survey_id and category_id is what I need)
| 1 | survey_id | date_created | date_updated | opening_date | closing_date | category_id | topic_id |
| 2 | 33 | Not important | Not important | NULL | NULL | 1 | NULL |
| 3 | 45 | Not important | Not important | NULL | NULL | 6 | NULL |
| 4 | 46 | Not important | Not important | NULL | NULL | 4 | NULL |
Try this query :
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
/*create table*/
}
else
// do something else

display table group by category using php

my database design is like this
-- Table Reason
--------------------------
| reasonid | reasonname |
--------------------------
| 1 | reason1 |
| 2 | reason2 |
--------------------------
-- Table Student
----------------------------
| studentid | studentname |
----------------------------
| 1 | John |
| 2 | Jane |
| 3 | Hulk |
----------------------------
-- Table form
-----------------------------------
| formid | studentid | reasonid |
-----------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 1 |
-----------------------------------
I want to show data table like :
reason1
| 1 | John |
| 2 | Hulk |
reason2
| 1 | Jane |
I have tried below code but the result is not groupBy reason
<?php $i =1;
while($rowfet = mysql_fetch_array($myselect)){ ?>
<h2><?php echo $rowfet['ReasonName']; ?></h2>
<table>
<thead>
<tr class="active">
<th>No.</th>
<th>StudentName</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $rowfet['STUDENTNAME']; ?></td>
</tr>
</tbody>
</table>
<?php $i++; } ?>
the result of this code is :
reason1
| 1 | John |
reason1
| 2 | Hulk |
reason2
| 3 | Jane |
You could do something like this:
outside while loop:
$lastReasonId = '';
inside while loop:
if($rowfet['reasonid'] != $lastReasonId){
// show heading
}
$lastReasonId = $rowfet['reasonid'];
But your code is far from ok. The $i++ thing is not doing anything, you should probably echo $rowfet['studentid'] there.
And you are using deprecated mysql code.

pulling from a specific data from a row and column in a database

My data base looks like this.
its ordered ascending by NO#
And col2 is the start of the database NO# is basically invisible and only used as a reference as to row number
so lets say I wanted to display on a web page the text in col8, row 5. What would the php code be?
PS. the connect code is seperate and not an issue hence i did not include itI
-|NO#|col2|col3|col4|col5|col6|col7|col8|col9|col10
---------------------------------------------------
|1 | | | | | | | | | |
---------------------------------------------------
|2 | | | | | | | | | |
---------------------------------------------------
|3 | | | | | | | | | |
---------------------------------------------------
|4 | | | | | | | | | |
---------------------------------------------------
|5 | | | | | | |2012| | |
---------------------------------------------------
|6 | | | | | | | | | |
---------------------------------------------------
|7 | | | | | | | | | |
---------------------------------------------------
|8 | | | | | | | | | |
---------------------------------------------------
|9 | | | | | | | | | |
---------------------------------------------------
|10 | | | | | | | | | |
---------------------------------------------------
Here is my code but it whites out the page when I try to load it.
<?php
//selects row
$query = "SELECT * FROM `Inventory` WHERE NO# = '5'";
//select column
$col8 = $row['col8'];
// fetch the results
WHILE($row = mysql_fetch_array($query):
$row = mysql_fetch_array($result);
// display the results
<div id="year">echo "$col8";</div>
?>
I would probably do something like what's below. I have not tested this code, though.
<?php
$result = mysql_query( "SELECT `col8` FROM `Inventory` WHERE `NO#` = '5' LIMIT 1" );
$row = mysql_fetch_assoc( $result );
?>
<div id="year"><?php echo $row['col8']; ?></div>
Hopefully that'll help you out a bit.

Need help ordering data from mysql_fetch_array()

I'm having a hard time organizing the data that I get from mysql_fetch_array().
I have a DB table with that looks something like this:
+---------------------+------------------+---------------+--------+---------+
| date | name | indexed_pages | nameID | entryID |
+---------------------+------------------+---------------+--------+---------+
| 2012-06-15 21:18:06 | site1.com | 200 | 1 | 1 |
| 2012-06-15 21:18:10 | site2.com | 25 | 2 | 1 |
| 2012-06-15 21:18:13 | site3.com | 12 | 3 | 1 |
| 2012-06-15 21:18:16 | site4.com | 8 | 4 | 1 |
| 2012-06-15 21:18:19 | site5.com | 2 | 5 | 1 |
| 2012-06-16 00:11:12 | site1.com | 191 | 1 | 2 |
| 2012-06-16 00:11:21 | site2.com | 25 | 2 | 2 |
| 2012-06-16 00:11:30 | site3.com | 12 | 3 | 2 |
| 2012-06-16 00:11:44 | site4.com | 8 | 4 | 2 |
| 2012-06-16 00:11:51 | site5.com | 2 | 5 | 2 |
| 2012-06-18 10:20:47 | site1.com | 191 | 1 | 3 |
| 2012-06-18 10:20:52 | site2.com | 25 | 2 | 3 |
| 2012-06-18 10:20:56 | site3.com | 12 | 3 | 3 |
| 2012-06-18 10:21:00 | site4.com | 8 | 4 | 3 |
| 2012-06-18 10:21:04 | site5.com | 2 | 5 | 3 |
+---------------------+------------------+---------------+--------+---------+
I need to order the results in a Google Line Graph in the following manner:
['date', 'site1_entryID=1', 'site2_entryID=2', 'site3_entryID=3', (...)],";
The thing is that I'm having trouble managing the arrays that I generate. I'm using the following code:
mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$query = "SELECT * FROM pages";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
After this I need to echo the number of indexed_pages for each site where entryID = 1.
I don't know if this description is confusing or not, but I've tried pretty much everything and can't get the organize the data from the arrays to serve what I need to do. Help, please!
Thanks in advance!
Don't use select *, that's lazy, and you're stuck accepting the fields in the order the DB decides to produce them in.
Specify the fields you want, in the order you want:
SELECT date, name, indexed_pages, etc...
I think the simplest query is :
$result= mysql_query("SELECT name, index_pages, entryID from table_name WHERE entryID =
1");
while($row=mysql_fetch_array($result)){
echo "$row[name]";
echo "$row[index_pages]";
echo "$row[entryID]";
}
Try this. There might be some mistakes. Because i developed it fast. And replace table_name with yours.
Or you can display it in a table:
echo "<table>";
echo "<tr><td>Sit Name</td>";
echo "<td>Page Name</td>";
echo "<td>EntryID</td>";
echo "</tr>";
while($row=mysql_fetch_array($result)){
echo "<tr>";
echo "<td>$row[name]</td>";
echo "<td>$row[index_pages]</td>";
echo "<td>$row[entryID]</td>";
echo "</tr>";
}
echo "</table>";
SELECT date, name, indexed_pages
FROM pages
where entryID=1
order by date asc ,name asc
Not sure if this will help
mysql_connect("host_here", "username_here", "pass_here") or die(mysql_error());
mysql_select_db("my_database") or die(mysql_error());
$query = "SELECT * FROM pages";
$result = mysql_query($query);
$data[]='date';
while($row = mysql_fetch_assoc($result)){
$name=substr($row['name'], -4);
$data[]= $name."_entryID=".$row['entryID'];
}
A little of a brute force method.

Reformatting the query

I have a table called sym_dis.
select * from sym_dis gives
+--------------+-----------------------------------+
| disease | symptom |
+--------------+-----------------------------------+
| typhoid | headache |
| typhoid | high fever |
| typhoid | pain in the abdomen |
| typhoid | sore throat |
| typhoid | feeling of fatigue |
| typhoid | weekness |
| typhoid | constipation |
| polio | headache |
| polio | nausea |
| polio | vomiting |
| polio | general discomfort |
| polio | slight fever for upto three days |
| polio | stiffness |
| polio | fever |
| polio | difficulty swallowing |
| polio | muscle pain and spasms |
| yellow fever | high fever |
| yellow fever | chills |
| yellow fever | headache |
| yellow fever | muscle ache |
| yellow fever | vomiting |
| yellow fever | backache |
| hepatitis B | jaundice |
| hepatitis B | fatigue |
| hepatitis B | abdominal pain |
| hepatitis B | loss of appetite |
| hepatitis B | nausea |
| hepatitis B | vomiting |
| hepatitis B | joint pain |
| hepatitis B | dark coloured wine |
| hepatitis B | yellowish tinged skin and eyes |
+--------------+-----------------------------------+
How can I reformat the above table using php and html so that I get the following output?
+--------------+-----------------------------------+
| disease | symptom |
+--------------+-----------------------------------+
| typhoid | headache |
| | high fever |
| | pain in the abdomen |
| | sore throat |
| | feeling of fatigue |
| | weekness |
| | constipation |
| polio | headache |
| | nausea |
| | vomiting |
| | general discomfort |
| | slight fever for upto three days |
| | stiffness |
| | fever |
| | difficulty swallowing |
| | muscle pain and spasms |
| yellow fever | high fever |
| | chills |
| | headache |
| | muscle ache |
| | vomiting |
| | backache |
| hepatitis B | jaundice |
| | fatigue |
| | abdominal pain |
| | loss of appetite |
| | nausea |
| | vomiting |
| | joint pain |
| | dark coloured wine |
| | yellowish tinged skin and eyes |
+--------------+-----------------------------------+
Something like:
$result = mysql_query("select * from sym_dis order by disease");
$lastDisease = '';
echo "<table>";
while ($row = mysql_fetch_assoc($result)) {
echo "<tr><td>";
if ($row['disease'] != $lastDisease)
{
echo $row['disease'];
$lastDisease = $row['disease'];
}
echo "</td><td>";
echo $row['symptom'];
echo "</td></tr>";
}
echo "</table>";
I wasn't sure if you wanted an html table, or actually the dashes and + style.
<?
foreach( $rows as $row ){
if( $row[0] != $last )
echo $row[0] . " ";
echo $row[1]
$last = $row;
}
?>
or similar
You need to get all "disease" and the, for each "disease" collect the matching "symptom", like this (working and tested):
<?php
define("HOST", "localhost");
// Database user
define("DBUSER", "username");
// Database password
define("PASS", "password");
// Database name
define("DB", "database_name");
############## Make the mysql connection ###########
$conn = mysql_connect(HOST, DBUSER, PASS) or die('Could not connect !<br />Please contact the site\'s administrator.');
$db = mysql_select_db(DB) or die('Could not connect to database !<br />Please contact the site\'s administrator.');
$query = mysql_query(" SELECT DISTINCT disease FROM sym_dis ");
echo '<table>';
while ($data = mysql_fetch_array($query)) {
echo '<tr><td valign="top">'.$data["disease"].'</td><td>';
$query2 = mysql_query(" SELECT * FROM sym_dis WHERE disease = '".$data['disease']."' ");
while ($data2 = mysql_fetch_array($query2)) {
echo $data2["symptom"] . '<br>';
}
echo '</td></tr>';
}
echo '</table>';
?>

Categories