I'm trying to design a webpage where it simply shows info from a database using PHP.
I almost solve all of my problem but it's the first time I'm using HTML and I am having trouble finding a solution for my problem.
The code I have so far is:
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo "<table>";
echo "<table border = 1>";
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo "<td>" . $row['piloto'] . "</td></tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td></tr>";
}
}
The output I'm getting can be seen in www.cardata.pt
1st problem: How to I make the "piloto" (for example AA) occupy the space of 2 cells?
2nd problem: I want the table to show "pilotos" side by side and the info for each one (tempo and data) down the piloto name.
Thanks in advance
To occupy the space of 2 cells add : colspan="2"
here is my edit of your code :
<?php
$connection = mysql_connect('localhost','XXXX','XXXX');
mysql_select_db('cardata_laptimer');
echo '<table border="0"><tr>';
$query = "SELECT * from tempos GROUP BY piloto";
$result = mysql_query($query);
while($row = mysql_fetch_array($result))
{
echo '<td><table border="1"><tr>';
$query = "SELECT * from tempos WHERE piloto = '" . $row['piloto'] . "' ORDER BY tempo";
$resultado = mysql_query($query);
echo '<td colspan="2">' . $row['piloto'] . "</td></tr><tr>";
while($rows = mysql_fetch_array($resultado))
{
echo "<td>" . $rows['tempo'] . "</td><td>" . $rows['data'] . "</td>";
}
echo '</tr></table></td>';
}
echo '</tr></table>';
?>
Related
It only shows 'No record found' but I'm trying to show the records between the dates using search
<?php
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary ";
// Date filter
if (isset($_POST['search'])) {
$fromDate = $_POST['fromDate'];
$endDate = $_POST['endDate'];
if (!empty($fromDate) && !empty($endDate)) {
$query = "SELECT title, count(title) as totalnotary ,
notary_date, book_no
FROM tbl_notary
Where 1
and notary_date between '" . $fromDate . "' and '" . $endDate . "'";
}
}
// Sort
$query .= " group by book_no,title,Year(notary_date),
month(notary_date),day(notary_date)
ORDER BY notary_date DESC";
$Records = mysqli_query($conn, $query);
// Check records found or not
if (mysqli_num_rows($Records) > 0) {
while ($Record = mysqli_fetch_assoc($Records)) {
$book_no = $Record['book_no'];
$title = $Record['title'];
$totalnotary = $Record['totalnotary'];
$notary_date = date("F j,Y", strtotime($Record['notary_date']));
echo "<tr>";
echo "<td>" . $book_no . "</td>";
echo "<td>" . $title . "</td>";
echo "<td>" . $totalnotary . "</td>";
echo "<td>" . $notary_date . "</td>";
echo "</tr>";
}
} else {
echo "<tr>";
echo "<td colspan='4'>No record found.</td>";
echo "</tr>";
}
?>
OK, so you are saying that here is your problem.
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary
Where 1 //!!!!!What is this 1???? You are missing a column name probably
and notary_date between '" . $fromDate . "' and '" . $endDate . "'";
Try this query. You have WHERE 1 which can't do anything and probably is failing your query. Try with some error reporting and you will probably get an error there.
$query = "SELECT title, count(title) as totalnotary , notary_date, book_no
FROM tbl_notary
WHERE notary_date BETWEEN '" . $fromDate . "' AND '" . $endDate . "'";
Then put this below to see if query is failing or not.
$Records = mysqli_query($conn, $query) or die (mysqli_error($conn));
I have php generated table that displays data as follow;
ID Name
1 xxxx
2 xxxx
I would like to be able to click on ID number and display information associated with the ID on separate page
Ive got so far:
table.php
include("connection.php");
$con=mysql_select_db('fm', $con);
$query = "SELECT * FROM table ;
$result = mysql_query($query);
echo "<table>
<tr>
<th>ID</th>
<th>Location</th>
</tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td><a href='send.php?=" . $row['id'] . "'>" . $row['id'] . "</a></td><td>" . $row['location'] . "</td></tr>";
}
echo "</table>";
mysql_close();
?>
info.php
include ("connection.php");
$con=mysql_select_db('fm', $con);
$id=$_GET['id'];
$query = "SELECT * FROM table WHERE id=". $id;
$result = mysql_query($query) or die (mysql_error());
echo "<table>
<tr>
<th>ID</th>
<th>Property</th>
<th>Location</th>
<tr>";
while($row = mysql_fetch_array($result)){
echo "<tr><td>" . $row['id'] . "</td><td>" . $row['property'] . "</td></td>" . $row['location'] . "</td></tr>";
var_dump($row);
}
echo "</table>";
$result = mysql_query($sql);
mysql_close($con);
Any input will be appreciated
you have got a mistake here :
echo "<tr><td><a href='info.php?id=" . $row['id'] . "</td><td>" . $row['location'] . "</td></tr>";
you need to end the <a> element like this:
echo "<tr><td><a href='info.php?id=".$row['id']."'>".$row['id']."</a></td><td>" . $row['location'] . "</td></tr>";
Also you should use IF statement in the info.php because if you access it like :
info.php // without ?id=
you will have undefined variable $id. And its vulberable to mysql injection when you dont process the variable before using it in select.
You got mistake here in info.php in this line:
$result = mysql_query($query, $con);
it should look like this :
$result = mysql_query($query);
You got it right in table.php.
I would change the echo command like this
echo "<tr><td>{$row['id']}</td><td>{$row['property']}</td></td>{$row['location']}</td></tr>";
The double quotes will take care of the values of the variables.
I'm new to PHP & MySQL and was trying to use php to create query for MySQL below is the current state of the code and it's still WIP but the if/elseif structure already gives the desired output which varies according to data input, which for example could be as follows
"SELECT * FROM tuntikortti WHERE PVM BETWEEN '2014-09-11' AND '2014-09-27' AND Henkilo =
'Van' AND Tyonumero = '123456' AND Toiminto = '123';"
I've confirmed that the query line itself works in MySQL but I would need to somehow get it inside the MySQL_query($con, The query here).
So how do I get the php output of whole if/elseif query line build in variable or similar to be used?
<?php
include("db_connect.php");
$pvm1 = $_POST['pvm1'];
$pvm2 = $_POST['pvm2'];
$henkilo = $_POST['henkilo'];
$tyonumero = $_POST['tyonumero'];
$toiminto = $_POST['toiminto'];
/* MySQL query line build, beginning */
ECHO "SELECT * FROM tuntikortti WHERE ";
/* PVM */
if(!empty($pvm1) && !empty($pvm2)){
echo "PVM BETWEEN '". $pvm1 ."' AND '". $pvm2 ."'";
} elseif(!empty($pvm1)) {
echo "PVM = '". $pvm1 ."'";
};
/* Henkilo */
if(!empty($henkilo) && !empty($pvm1)){
echo " AND Henkilo = '". $henkilo ."'";
} elseif(!empty($henkilo)) {
echo " Henkilo = '". $henkilo ."'";
};
/* Tyonumero */
if(!empty($tyonumero) && (!empty($pvm1) || !empty($henkilo))){
echo " AND Tyonumero = '". $tyonumero ."'";
} elseif(!empty($tyonumero)) {
echo " Tyonumero = '". $tyonumero ."'";
};
/* Toiminto */
if(!empty($toiminto) && (!empty($tyonumero) || !empty($pvm1) || !empty($henkilo))){
echo " AND Toiminto = '". $toiminto ."'";
} elseif(!empty($toiminto)) {
echo " Toiminto = '". $toiminto ."'";
};
echo ";";
$query = mysqli_query($sql,$con);
echo "<table border='1'>
<tr>
<th>TID</th>
<th>PVM</th>
<th>Henkilo</th>
<th>Tyƶnumero</th>
<th>Toiminto</th>
<th>Tunnit</th>
<th>Selite</th>
</tr>";
while($row = mysqli_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['TID'] . "</td>";
echo "<td>" . $row['PVM'] . "</td>";
echo "<td>" . $row['Henkilo'] . "</td>";
echo "<td>" . $row['Tyonumero'] . "</td>";
echo "<td>" . $row['Toiminto'] . "</td>";
echo "<td>" . $row['Tunnit'] . "</td>";
echo "<td class='selitetyyli'>" . $row['Selite'] . "</td>";
echo "</tr>";
}
echo "</table>";
include("db_close_connection.php");
?>
Check this example and try to modify your code,
$sql = '';
$sql = "SELECT * FROM tablename WHERE ";
if(condition){
$sql.= "fieldname = 'value'";
} elseif(condition) {
$sql.= "fieldname = 'value'";
};
$sql.= ";";
$query = mysqli_query($con,$sql);
I've commented out the bottom part, and the SQL query works fine. Its the displaying of the query where the error is coming from i believe.
$host = "127.0.0.1";
$user = "root";
$pass = "Toom13371!";
$connection = mysql_connect($host,$user,$pass) or die (mysql_errno().": ".mysql_error()."<BR>");
// 2. Selecting DB.
$dbname = "filters";
mysql_select_db($dbname);
// 3. Build/Test SQL Query
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
//echo $sql; //Comment/Uncomment to test sql query.
// 4. Retrieve info from MySQL.
$query = mysql_query($sql);
// 5. Display Query.
echo "<table border='1'>
<tr>
<th>Low Frequency</th>
<th>High Frequency</th>
</tr>";
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
Any help would be appreciated, I'm sure it's going to be some small stupid error i've over looked.
Thanks in advance :)
I'm guessing, based on your query, that you need to change this
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
and
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['Lowfreq'] . "</td>";
echo "<td>" . $row['Highfreq'] . "</td>";
echo "</tr>";
}
to
while($row = mysql_fetch_array($query)) {
echo "<tr>";
echo "<td>" . $row['start_passband'] . "</td>";
echo "<td>" . $row['stop_passband'] . "</td>";
echo "</tr>";
}
Change line
mysql_select_db($dbname);
to
mysql_select_db($dbname, $connection);
Also before query check
$_POST['Lowfreq'] and $_POST['Highfreq']
If there is no value in these variables query will must return empty.
In your query there should not brackets for string.
$sql = ("select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'");
should be:
$sql = "select * from filter_bandpass where start_passband=" . $_POST['Lowfreq'] . " and stop_passband='" . $_POST['Highfreq'] . "'";
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