How to add bootstrap grid with database result using php - php

I am newbie in php. i am using the following code to get my desired data from mysql database. but what i want to do, whenever database show a result i want to put it into a bootstrap grid( like col-sm-4) each time. Right now my grid is coded in HTML, how can i generate it with the query result each time ? thanks in advance.
<div class="col-sm-4">
<h3>image </h3><br>
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
$link = "http://localhost/sc/uploads/" .$row[1];
echo "<img width='100%' height='200' src=$link />"."<br />";
echo "";
}
?>
</div>

Do you mean something like this where you just want to put each rows data into its own div
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<div class="col-sm-4">';
echo '<h3>image </h3><br>';
$link = "http://localhost/sc/uploads/" .$row[1];
echo '<img style="width:100%;height:200px" src="' . $link . '"/>';
echo '</div>';
}
?>
Just for future reference, its a bad idea to use the full url for your own site in code like this. If you move it to a live site this code wont work anymore. Better to use relative paths and let the system do some of the work for you. So it should work whatever the domain is where you move the code.
<?php
$sql = "SELECT * FROM sctable";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result))
{
echo '<div class="col-sm-4">';
echo '<h3>image </h3><br>';
> Changed next line
$link = "sc/uploads/" .$row[1];
echo '<img style="width:100%;height:200px" src="' . $link . '"/>';
echo '</div>';
}
?>

You can just wrap the image:
echo "<div class='col-sm-4'> <img width='100%' height='200' src='$link' /></div>";

Related

How to fetch all datas and images from mysql tableusing PDO?

I'm trying to get a list of news from MySQL but I have some issues with PDO and I can not get. Actually I have titles, messages, photos and categories. Please see the code below and if you can help me. Images are in folder named 'userdatas'
$sth = $conn->prepare("SELECT * FROM news");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
echo $row['id'];
echo $row['title'];
echo $row['category'];
echo $row['message'];
echo '<img src="' . $row['photo'] . '" height="60" width="40"> ';
echo "<br>";
}
The problem with the above code is that you are only specifying the image name. You need something that will specify the full path to the image like this:
$sth = $conn->prepare("SELECT * FROM news");
$sth->execute();
while($row = $sth->fetch(PDO::FETCH_ASSOC)){
echo $row['id'];
echo $row['title'];
echo $row['category'];
echo $row['message'];
echo '<img src="PATH/TO/IMAGE/' . $row['photo'] . '" height="60" width="40"> ';
echo "<br>";
All you need to do is replace PATH/TO/IMAGE with the actual folder path, or change the database to include the full path

Error with image display

Please check below code and help me out if anything is going wrong in image display code
Untitled Document
<body>
<p>how to display image</p>
<p>image is not seeingd</p>
<?php
$con= mysql_connect("localhost","root","");
$d= mysql_select_db("matrimonial",$con);
$id=$_REQUEST['id'];
$sql = "select * from advertiesment where S_NO ='1'" or die(mysql_error());
$result = mysql_query($sql,$con);
if($rows=mysql_fetch_assoc($result))
{
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
}
?><p>how to display image</p>
</body>
</html>
if($rows=mysql_fetch_assoc($result))
The condition is bad. If you have more results from SQL query,you need to use while.
while ($rows = mysql_fetch_assoc($result)) {...
If you have only one, remove just if:
$rows = mysql_fetch_assoc($result);
echo ...
EDIT:
$image = $rows['filepath'] . '/' . $rows['filename'];
Dont use :
if($rows=mysql_fetch_assoc($result))
{
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";
}
use this directly
$rows=mysql_fetch_assoc($result)
$image = $rows['filepath'];
echo "<img style='width:150px;height:150px' src='$image'>";
echo "<br>";

Mysql Field Data not displaying when a link is clicked?

I'm trying to get data from a database if a link is clicked.
I used the example codes suggested from this example -Getting mysql field data when a link is clicked?
But it doesn't work when I click on a link nothing comes up.
main.php
<?php
include('conn.php');
$sql2 = "SELECT Title FROM addpromo";
$result2 = mysql_query($sql2);
echo "<div id=\"links\">\n";
echo "<ul>\n";
while ($row2 = mysql_fetch_assoc($result2)) {
echo "<li> <a href=\"fullproject.php?title=\""
. urlencode($row2['Title']) . "\">"
. htmlentities($row2['Title']) . "</a>\n</li>";
}
echo "</ul>";
echo "</div>";
?>
This is displaying correct.but when I click at a link nothing is showing up in fullproject.php, Just a blank page.
fullproject.php
<?php
// Connect to server.
include('conn.php');
$projectname = isset($_GET['Title']);
$sql1 = "SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1 = mysql_query($sql1);
while ($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title'] . "<br />";
echo "<br /> ";
}
?>
Can someone help me to fix this, or any other way to make this(to get data from a database if a link is clicked) possible?
Change to this
main.php
<?php
include('conn.php');
$sql2="SELECT Title FROM addpromo";
$result2=mysql_query($sql2);
echo '<div id="links">';
echo '<ul>';
while($row2 = mysql_fetch_assoc($result2)){
echo '<li>'.htmlentities($row2['Title']).'</li>';
}
echo '</ul>';
echo '</div>';
?>
fullproject.php
<?php
if(isset($_GET['title'])){
include('conn.php');
$projectname= $_GET['title'];
$sql1="SELECT Title FROM addpromo WHERE Title = '$projectname'";
$result1=mysql_query($sql1);
while($row1 = mysql_fetch_assoc($result1)) {
echo "Project Name: " . $row1['Title']. "<br />";
echo "<br /> ";
}
}
?>
This is storing a boolean value $projectname= isset($_GET['Title']);, whether or not the title is set. Instead use $projectname = $_GET['Title'];
isset returns a boolean value (true/false) and you want the actual value of the variable:
$projectname= $_GET['title'];
Furthermore, you have to pass only the title as the URL parameter, without enclosing it within quotes. So there is an error in this line:
echo "<li> <a href=\"fullproject.php?title=" . urlencode($row2['Title']) . "\">"
Note the lack of \" after title=

Place php code inside floating css container(s)?

i have a question in regards to php and css layers.
i have the following php code:
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
//while ($db_field = mysql_fetch_assoc($result)) {
//print $db_field['planet_name'] . "<BR>";
//print $db_field['location'] . "<BR>";
while($row = mysql_fetch_array( $result )){
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo "<br> ";
}
which correctly displays a word [Planet name] and a number [System] in sequence.
The above code displays the information in rows such as;
Planet Sun is located in system 35.
Planet Saturn is located in system 30.
i'm just trying to make this information display a look a little nicer. in a way so planet name shows up in the right of a background image container and system in another corner possibly colored.
....
How do i place the above code inside floating css container(s)?
Thank you.
As it seems to be a list of planets, I would use an HTML list. You can edit the css to have it look the way you want after.
echo ' <ul class="planetList"> ';
while($row = mysql_fetch_array( $result )){
echo '<li>';
echo " Planet, ".$row['planet_name'];
echo " is located at System ".$row['location'];
echo '</li> ';
}
echo '</ul>';
In CSS
ul.planetList {
display:block;
float:right;
background-image:url('yourBackground.jpg');
background-position:left;
background-repeat:repeat-y;
/* CSS 3 Only */
background-size:{length of your text}px 100%;
}
You could also use a table instead of the list. This way, you could get a background for the planet column, and another one for the located at sytem one.
echo ' <table> ';
echo ' <tr><th>Planet</th><th>System</th></tr>';
while($row = mysql_fetch_array( $result )){
echo '<tr>';
echo '<td class="planet">' . $row['planet_name'] . '</td>';
echo '<td class="system">' . $row['location'] . '</td>';
echo '</tr> ';
}
echo '</table>';
CSS :
table>tr.planet {
background-image:url('yourBackground.jpg');
background-position:left;
}
table>tr.system {
background-color:#CCFF00;
}
Php code should be inside tags. You can always close these tags and put some htmls tags
Example:
<div class="bla"><?php
my code
?></div>
Obviusly you can do everything in php
<?php
echo '<div class="bla">';
....phpcode....
echo '</div>';
?>
Hopefully this helps
You can do something like this to keep your code clean
<?php
session_start();
// Retrieve all the data from the table
$SQL = "SELECT * FROM Exodus_planets WHERE login_id = $user[login_id] LIMIT 10";
$result = mysql_query($SQL);
$planets = array();
while($row = mysql_fetch_array( $result )){
$planets[] = $row;
}
foreach($planets as $planet): ?>
<div class="prettyFloatyDiv">
Planet, <?php echo $planet['planet_name']?>
is located at System <?php echo $planet['location']?>
<br/>
</div>
<?php endforeach; ?>
If you have short tags enabled, you can replace every <?php echo with <?=

PHP MYSQL BASIC QUESTION, IF QUERY EMPTY, HIDE IMAGE

I ask a php mysql basic question. I make a query and echo for images. in my code, if '.$row['image'].' is empty, it will show a small red cross image in the browser. how to hide the image if the query is empty?
$result = mysql_query("SELECT * FROM table WHERE catalog='image'");
while ($row = mysql_fetch_array($result))
{
echo '<img src="'.$row['image'].'" />';
}
$result = mysql_query("SELECT * FROM table WHERE catalog='image'");
while ($row = mysql_fetch_array($result))
{
if(!empty($row['image'])){
echo '<img src="'.$row['image'].'" />';
}
}
Why don't you just check if $row['image'] is empty?
if(!empty($row['image'])){
echo '<img src="'.$row['image'].'" />';
}
$result = mysql_query("SELECT * FROM table WHERE catalog='image'");
while ($row = mysql_fetch_array($result))
{
if(trim($row['image']) != '')
echo '<img src="'.$row['image'].'" />';
}

Categories