I am trying to get information from mysql including a blob image which i shall echo with php and will have an onclick event within the php, redirecting it to another page. The onlick event will contain a mysql result which it will carry with it as seen in the code below.
My main issue is with the syntax of the code or if there is another way to do it all together. please keep in mind the output when the script is run is similiar to that of google images, bing images etc. Thank you.
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
echo "<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td style='padding-right: 2em;'><img src="data:image/jpeg;base64,' . base64_encode( $row['image'] ) . '" width="160px" height="200px";" onclick="window.location='lookup.php?pattern=" . $row['title'] . "';>";
</td>
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Untested but here is one way you could clean up your code move style and javascript (and use some jquery) into the head:
<?php
$con=mysqli_connect("localhost","root","*******","media") or die("Failed to connect to MySQL: " . mysqli_connect_error());
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<html>
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script>
$(document).ready(function() {
$('img').each(function() {
var img = $(this);
img.click(function() {
window.location="lookup.php?pattern=" + img.attr('title');
});
});
});
</script>
<style>
table {
margin: auto;
text-align: left;
background: white;
padding: 3em;
border: 2px solid #000000;
}
table tr td {
padding-right: 2em;
}
table tr td img {
width: 160px;
height: 200px;
}
</style>
</head>
<body>
<table>
<tr>
<th>Movie Title</th>
<th>Language</th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
echo "
<tr>
<td>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\" title=\"" . $row['title'] . "\">
</td>
</tr>
";
}
?>
</table>
</body>
</html>
<?php mysqli_close($con); ?>
Or if you don't want to use javascript, you could always wrap the image around the anchor tag instead:
<td>
<a href='lookup.php?pattern={$row['title']}'>
<img src=\"data:image/jpeg;base64," . base64_encode( $row['image'] ) . "\">
</a>
</td>
You could further separate PHP and HTML code:
<?php
$con=mysqli_connect("localhost","root","*******","media");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
die();
}
$result = mysqli_query($con,"SELECT * FROM movies ORDER BY `movies`.`title` ASC");
?>
<table border='3' style='margin: auto; text-align: left;background: white; padding: 3em;'>
<tr>
<th><b>Movie Title</b></th>
<th><b>Language</b></th>
</tr>
<?php
while($row = mysqli_fetch_array($result)) {
$img = 'data:image/jpeg;base64,'
. base64_encode( $row['image'] );
?>
<tr>
<td style='padding-right: 2em;'>
<img src="<?php echo $img; ?>"
style="width: 160px; height: 200px;"
onclick="window.location='lookup.php?pattern=<?php echo $row['title']?>;'"
/>
</td>
</tr>
<?php } ?>
</table>
<?php
mysqli_close($con);
?>
You could also use some sort of templating engine to do this, but the results would be pretty much the same - I don't see much point in writing, say,
<strong>{{ title }}</strong>
instead of
<strong><?php echo $title; ?></strong>
Related
I have a problem in inserting table from my database to div.
html code:
<div class="content">
<?php
$query = "SELECT name, surname FROM players";
$response = #mysqli_query($dbc, $query);
if($response) {
echo' <table align="left"
cellspacing="5" cellpadding="8" >
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td></tr>';
while($row = mysqli_fetch_array($response)) {
echo '<tr><td align="left">' .
$row['imie'] . '</td><td align="left">' .
$row['nazwisko'] . '</td><td align="left">';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</div>
css code:
.content {
width: 1000px;
margin-left: auto;
margin-right: auto;
background-color: #ffffff;
color: #000000;
border-bottom: 14px solid #333333;}
Is there a way to insert this table to this div? Because when I'm loading this, the table is under my div'content'. The table should be in blank field 'a'.
Screenshot
Correctly indenting your code would have allowed you o spot the error relatively easily - there is an open td element. Either remove the last td from the echo statement in the loop or add additional td pair to first row in table so that they balance ( unless you use colspan attributes )
<style>
.content table tr td{ align:left;padding:5px;color:black;background:white; }
</style>
<div class='content'>
<?php
$sql = 'select `name`, `surname` from `players`';
$response = mysqli_query( $dbc, $sql );
if( $response ) {
echo "
<table align='left' cellspacing='5' cellpadding='8' >
<tr>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
</tr>";
while( $row = mysqli_fetch_array( $response ) ) {
echo "
<tr>
<td>{$row['imie']}</td>
<td>{$row['nazwisko']}</td>
</tr>";
}
echo "
</table>";
} else {
printf( 'Couldn\'t issue database query: %s', mysqli_error( $dbc ) );
}
mysqli_close( $dbc );
?>
</div>
There is an open <td> tag in your code you should close it before closing the <tr>
I need my table to include submissions into three columns then a new row start with three more columns, etc. So submission 1, first column, submission 2, second column, submission 3, 3 column. Then a new row and the process starts again. (HTML Example Code below)
Every time I try with my php code my page comes up blank so I am missing something somewhere.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT id, name, website, url, email, date, description FROM basic";
$result = $conn->query($sql);
if ($result->num_rows>0){
<tr>
while ($row=$result->fetch_assoc()) {
echo "<table><tr><th><b>Company:</b> " . $row["name"]. "</th></tr><tr><td>
<b>URL:</b> <a href='".$row["url"]."'>".$row["url"]."</a></td></tr><tr><td>
<b>Email:</b> " . $row["email"]. "</td></tr><tr><td><b>Launch Date:</b> " .
$row["date"]. "</td></tr><tr><td><b>Announcement:</b> " .
$row["description"]. "</td>
</tr>";
}
echo "</table>";
} else {
echo "0 results";
}
$conn->close();
?>
Example: This is what I am trying to get it to look like.
<style>
table, td, th {
border: 10px solid #ffffff;
text-align: left;
}
table {
border-collapse: initial;
width: 100%;
}
td {
padding: 15px;
width: 25%;
line-height: 2;
}
th {
background-color: grey;
color: white;
padding: 5px;
width: 25%;
}
</style>
<body>
<table class="ex1">
<tr>
<th>Company</th>
<th>Company</th>
<th>Company</th>
</tr>
<tr>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
<td>
<b>URL:</b><br>
<b>Location:</b><br>
<b>Inductry:</b><br>
<b>Start Date:</b><br>
<b>Announcement:</b><br>
<br>
</td>
</table>
Your PHP code is not generating HTML in proper format. Please try below:
// Query executed here and resultset returned
if ($result->num_rows>0){
echo "<table>";
while ($row=$result->fetch_assoc()) {
echo "<tr><th><b>Company:</b> " . $row["name"]. "</th></tr>
<tr><td><b>URL:</b> <a href='".$row["url"]."'>".$row["url"]."</a></td></tr>
<tr><td><b>Email:</b> " . $row["email"]. "</td></tr>
<tr><td><b>Launch Date:</b> " . $row["date"]. "</td></tr>
<tr><td><b>Announcement:</b> " . $row["description"]. "</td></tr>";
}
echo "</table>";
} else {
echo "0 results";
}
I currently have a table in my lamp database which I would like to display products. However I have displayed the products, but the page looks un-professional. How to make the page more professional? I don't want to display the products in a table.
I have posted my code below:
<?php
$con = mysql_connect("localhost","sam","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("products", $con);
$result = mysql_query("SELECT * FROM Products_table WHERE catid IN (1,2,7,8)");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Image</th>
<th>Description</th>
<th>Contact Renter</th>
<th>Rent price</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['productname'] . "</td>";
echo "<td><img src='./images/products/".$row['productimage']."' width='150' height='100' alt=''></td>";
echo "<td>" . $row['productdescription'] . "</td>";
echo "<td>" . $row['rentersdetails'] . "</td>";
echo "<td>" . $row['rentprice'] . "</td>";
echo "</tr>";
echo <<<"buttons"
<td>
<input class="button_normal" type="button" value="Google Renter" onclick="window.open('https://www.google.co.uk/')"/>;
<input class="button_normal" type="button" value="Yahoo" onclick="window.open('https://www.yahoo.co.uk')"/>
</td>
buttons;
}
echo "</table>";
mysql_close($con);
?>
You can put the results in a variable:
<?php
$con = mysql_connect("localhost","sam","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("products", $con);
$result = mysql_query("SELECT * FROM Products_table WHERE catid IN (1,2,7,8)");
$results = array();
while($row = mysqli_fetch_assoc($query)){
$results[] = $row;
}
mysql_close($con);
?>
and call your $results array like so:
$results[0]['value']
or in a foreach
foreach($results as $row => $value) {
//display your data however you like
}
What do You mean 'unprofessional'? You can style Your table with css.
Or create grid with div's with block or inline-block display property.
Have you considered using BootStrap CSS? I know you don't want to use tables to display the data but adding BootStrap or other CSS templates will make it look less "ugly" and less like a "table". Other than that, perhaps use lists? or other data structures?
http://getbootstrap.com/css/#tables
Adding it should be pretty simple, you can even use the links from the CDN that they provide below. Hope this helps!
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap-theme.min.css">
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
try to use PDO and use fetchAll() :
http://php.net/manual/en/pdostatement.fetchall.php
$first = '';$rows=array();
while($row = mysql_fetch_array($result)){
if(!$first){
$first="<tr><th>".implode('</th><th>',array_keys($row)).'</th></tr>';
}
$rows[]="<tr><td>".implode('</td><td>',$row).'</td></tr>';
}
$table = "<table class="table">$first".implode("\n",$rows)."</table>";
For Simple Product HTML http://www.hongkiat.com/blog/html5-single-product-page/
<?php
$con = mysql_connect("localhost","sam","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("products", $con);
$result = mysql_query("SELECT * FROM Products_table WHERE catid IN (1,2,7,8)");
?>
<table style="border:1px solid;">
<tr style="background:#000;color:#fff;">
<th>Name</th>
<th>Image</th>
<th>Description</th>
<th>Contact Renter</th>
<th>Rent price</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['productname'];?></td>
<td><img src='./images/products/<?php echo $row["productimage"];?>' width='150' height='100' alt=''></td>
<td><?php echo $row['productdescription'];?></td>
<td><?php echo $row['rentersdetails'];?></td>
<td><?php echo $row['rentprice'];?></td>
</tr>
<?php
}
mysql_close($con);
?>
</table>
apply this code.... remember one thing more don't echo html tags. Due to echo/print html tags in php code it will slow your page speed. Your page will take more time for load.
You can post your data to a responsive table made of Div tags. It looks like a table in large screens and changes to individual elements in smaller mobiles:
<div id = "" class="csstable u-full-width">
<div class="cssthead">
<div class="cssth">ccid</div>
<div class="cssth">administration_title</div>
<div class="cssth">author_id</div>
<div class="cssth">date_created</div>
</div>
<div class="csstbody">
<div class="csstr">
<div class="csslabel">ccid</div><div class="csstd">ccid-5528b9495c6864.97942127</div>
<div class="cssnlbr"></div>
<div class="csslabel">administration_title</div><div class="csstd">ccid-5528b9495c6864.97942127-title</div>
<div class="cssnlbr"></div>
<div class="csslabel">author_id</div><div class="csstd">1</div>
<div class="cssnlbr"></div>
<div class="csslabel">date_created</div><div class="csstd">2015-04-11 08:03:53</div>
<div class="cssnlbr"></div>
</div>
<div class="csstr">
<div class="csslabel">ccid</div><div class="csstd">ccid-5528b992dacfc7.34608410</div>
<div class="cssnlbr"></div>
<div class="csslabel">administration_title</div><div class="csstd">ccid-5528b992dacfc7.34608410-title</div>
<div class="cssnlbr"></div>
<div class="csslabel">author_id</div><div class="csstd">1</div>
<div class="cssnlbr"></div>
<div class="csslabel">date_created</div><div class="csstd">2015-04-11 08:05:06</div>
<div class="cssnlbr"></div>
</div>
</div>
CSS:
/* Larger than tablet */
#media (min-width: 750px)
{ /* border collapse is required for any borders to appear */
.csstable { display: table; border-collapse: collapse; }
.csstr { display: table-row }
.cssthead { display: table-header-group; border-bottom: 2px solid #eaeaea; }
.csstbody { display: table-row-group }
.csstfoot { display: table-footer-group }
.csscol { display: table-column }
.csscolgroup { display: table-column-group }
.csstd { display: table-cell; padding: 4px; }
.cssth { display: table-cell; font-weight: 600; padding: 0 0 0 4px; }
.csscaption { display: table-caption }
.csslabel{ display: none; }
.cssnlbr{ display: none; }
.csstr:nth-child(even) {
background-color: #f1f1f1;
}
}
/* smaller than tablet */
#media (max-width: 750px)
{ /* border collapse is required for any borders to appear */
.csstable { display: block }
.csstr { display: block; border: 1px solid #e1e1e1; padding: 4px; margin-bottom: 4px;}
.cssthead { display: none }
.csstbody { display: block }
.csstfoot { display: none }
.csscol { display: block }
.csscolgroup { display: block }
.csstd { display: inline-block }
.cssth { display: none; font-weight: 600; }
.csscaption { display: table-caption }
.csslabel{ display: inline-block; font-weight: 600; padding-right:6px; }
.cssnlbr{ display: block; height: 3px;}
}
I don't have time to find and use a code parsing site. But you can see how it works here on my framework development site.
I'm making a page to register damage mobile phones. I made a search query to search data in database which matched datepicker's date. Here is my source code. (Only relevant to this question, )
<?php
$con = mysql_connect("localhost", "root", "");
if ($con) {
$db = mysql_select_db('mobile', $con);
} else {
die('Could not connect: ' . mysql_error());
}
if (array_key_exists('myday', $_POST)) {
$mydate = $_POST['mydate'];
$myday = "SELECT * FROM mobile_rep WHERE sdate='{$mydate}' ";
}
mysql_close($con)
?>
<html>
<head> </head>
<body>
<form action="index.php" method="POST" class="form-inline">
<div class="span12" style="border-radius: 5px; border-style: solid; border-width: 1px; margin-left: 20px;" >
<h5 style="margin-left: 10px; "> <b> Current User List </b></h5>
Select By Date
<input type="date" name="mydate" id="mydate" class="input-medium" />
<input type="submit" class="btn-primary " name="myday" value="Search by date" id='myday' />
<br/> <br/>
<table border="1" width="300" cellspacing="1" cellpadding="1" class="table table-striped" style=" border-radius: 5px; border-style: solid; border-width: 1px; ">
<thead>
<tr>
<th>sdate</th>
<th>model_no</th>
<th>fault</th>
<th>rp_fee</th>
<th>sp_fee</th>
<th>total</th>
</tr>
</thead>
<tbody>
<?php
while ($row1 = mysql_fetch_array($myday)) {
echo "<tr>";
echo "<td>" . $row1['sdate'] . "</td>";
echo "<td>" . $row1['model_no'] . "</td>";
echo "<td>" . $row1['fault'] . "</td>";
echo "<td>" . $row1['rp_fee'] . "</td>";
echo "<td>" . $row1['sp_fee'] . "</td>";
echo "<td>" . $row1['total'] . "</td>";
echo "</tr>";
}
?>
</tbody>
</table>
</div>
</form>
</body>
</html>
And I'm having
Undefined variable: myday in C:\wamp\www\mobile\index.php on line 227
Warning: mysql_fetch_array() expects parameter 1 to be resource, null given in C:\wamp\www\mobile\index.php on line 227
Line 227 means while ($row1 = mysql_fetch_array($myday)) {
I also uploaded an image of my page too.
Please help me.
When you enter your page first time, $myday variable is not set. If you send your POST code then if (array_key_exists('myday', $_POST)) { } is executed and $myday is created.
Just check before while loop if your variable exists, otherwise it pass null value to mysql_fetch_data function
if(isset($myday)) { while ($row1 = mysql_fetch_array($myday)) ... }
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while ($pF = mysql_fetch_array($stringVisits)) {
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if (checkStatus($BuID) == 1) {
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if (!empty($getByProfile["photo_thumb"])) {
echo $getByProfile["photo_thumb"];
} else {
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php } ?>
</tr>
</table>
This is what i have right now it displays the visiterĀ“s profileimage. Now i would like to have their name under the image too.. but then i need to create another <tr> after this one, and add their name in each <td>. But how can i do that, if i have this while? Should i run another while, to get the names or can i do something smart with this?
Why not just stick the name in the same cell as the image? After you close the image tag, just echo $getByProfile["username"], or whatever?
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
while($pF = mysql_fetch_array($stringVisits)){
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'><br/>";
?>
</a>
<br/><?php echo $getByProfile['username']; ?>
</td>
<?php } ?>
</tr>
</table>
Here's a way to do it with minimal changes to your existing code:
<?php
<table border="0" cellspacing="0" cellpadding="0" >
<tr>
<?php
$names = array(); // ADDITION #1
while($pF = mysql_fetch_array($stringVisits)){
$names[] = // ADDITION #2 - ADD NEW USER NAME HERE;
?>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<?php
echo "<a href='profil.php?id=".$BuID."'>";
echo "<img style='margin-right: 5px; width: 61px; height: 80px;'";
if(checkStatus($BuID) == 1){
echo 'class="onlineBorder" ';
} else {
echo 'class="image-xxsmall-border" ';
}
echo " src='images/profilePhoto/thumbs/";
if(!empty($getByProfile["photo_thumb"])) { echo $getByProfile["photo_thumb"]; }else{
echo "noPhoto_thumb.jpg";
}
echo "'>";
?>
</a>
</td>
<?php }
// ADDITION #3:
echo "</tr>
<tr>";
foreach ($names as $username)
echo "<td class=\"username\"><p>$username</p></td>";
?>
</tr>
</table>
Also note that the way you had your code, the </tr> was inside the while loop.
this might not look nice but it should work why do you want 2 loops?
<?php
echo "<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" >";
while($pF = mysql_fetch_array($stringVisits)){
echo "
<tr>
<td class="viewercell" style="color: #CCC; font-size: 10px;">
<a href='profil.php?id=".$BuID."'> <img /> </a>
</td>
</tr>
<tr><td> " . $getByProfile['username'] . " </td></tr>";
}
echo "</table>";
?>