How to move html/css content to a different column - php

Right now I have this program that echoes out an image and a text ten times. It does so all in one column. I want to make it so that it fills up one whole page by going horizontally after it hits the end of the page in one column. After all the columns fill up, then it can go to the next page. Here is my code:
while($row = mysqli_fetch_array($result)) {
echo '<div>';
echo "<p style='font-size:1.05em;color:#0e3c68;font-weight:bold;'>" . $row['question'] . "</p>";
<img src="data:image/jpeg;base64,'.$row['imagename'] .'" height="125" width="125" class="img-thumnail" />
echo '<div>';

Just your code as following.
echo '<div style="text-align: center;display: inline-block;">';
echo "<p style='font-size:1.05em;color:#0e3c68;font-weight:bold;'>" . $row['question'] . "</p>";
<img src="data:image/jpeg;base64,'.$row['imagename'] .'" height="125" width="125" class="img-thumnail" />
echo '<div>';

Related

PHP / MySQL - Query not fetching any data

I have been trying to set up a page that lists prices of items from a table in a database. Here is my code:
<?php
$querylist = mysql_query("SELECT item_name,image,price,added_by FROM values");
while($row = mysql_fetch_array($querylist))
{
echo '<div class="post rareitem" style="margin-right: 15px;float: left;">
<div class="rarename">
<strong>';
// Shows Item Name
echo $row['item_name'];
echo '</strong>
</div>';
// Shows Item Image
echo '<div class="rareimage" style="background-image: url(/app/tpl/skins/Mango/images/values/rares/';
echo $row['image'];
echo ');"></div>';
// Shows Item Price
echo '<div class="rarecontrols">
<div class="coinsbox"></div>
<span>
<b> <b>Credits: </b> </b> ';
echo $row['price'];
echo '</span>';
// Shows Who Added the Item
echo '<div class="addedbox"></div><b><b><span><font color="#c93734"><font color="#c93734">Added By: </font> </font>';
echo $row['added_by'];
echo '</span></b></b>
</div>
<div class="clear"></div>
</div>';
}
?>
There is another chunk of code (shown below) that I have based this off of, and it works perfectly fine. I can't seem to get this to work though. I believe it has something to do with the SQL, the syntax, or something. No matter what I do, it produces absolutely no results, yet the code below results exactly as planned. I know for a fact it is not a connection issue because the below code can be placed on the same exact page as the above one and it works fine, however the above does not.
<?php
$querylist = mysql_query("SELECT id,username,motto,country,look,rank,account_created,role,account_created,online,last_online FROM users WHERE rank='9' ORDER BY ID LIMIT 20");
while($row = mysql_fetch_array($querylist))
{
echo '
<div class="team">';
// Showing Avatar
echo '<div style="float: left; margin-top:-1px;height: 60px; width: 64px; background: url(http://www.habbo.nl/habbo-imaging/avatarimage?figure=';
echo $row['look'];echo "&action=wav&direction=3&head_direction=3&gesture=sml&size=m) no-repeat 0px -10px";
echo "\"/>";
echo "<img alt=\"badge\" src=\"/app/tpl/skins/habbo/images/icons/";
echo $row['online'];echo ".gif\"></div>";
// Flags
echo "<br/><img src=\"/app/tpl/skins/habbo/images/icons/flags/";
echo $row['country'];echo ".png";
echo '" style="float:right;" /> <b><uf>';
echo $row['username'];echo "</u></b>";
// Bans & Ticket Count
$Bans = mysql_query("SELECT * FROM `bans` WHERE `added_by` = '" . $row['username'] . "'");
$BanCount = mysql_num_rows($Bans);
$Tickets = mysql_query("SELECT * FROM `moderation_tickets` WHERE `moderator_id` = '" . $row['id'] . "'");
$TicketCount = mysql_num_rows($Tickets);
//Role
echo "<br/><gb>Role: </b><fi> ";
echo $row['role'];echo "";
echo "</i>";
// Echoing bans & Tickets
echo "<br/><gb>Bans: </b><fi> ";
; echo $BanCount;
echo "</i>";
echo " <gb>Tickets: </b><if>";
; echo $TicketCount;
echo "</i>";
echo "</div>";
}
?>
Thanks in advanced, any assistance will be greatly appreciated!
values is Reserved Words in mysql it should be on backtick
SELECT item_name,image,price,added_by FROM `values`
And stop using mysql it is deprecated. Instead use mysqli or PDO

How to show images from my sql to this table? in php

i have a pagination code in php ,i tried to show images post_images from my sql to this table in my code but nothing happen ,i tried echo '<img src= images/$post_image />';
,can you tell me please what i can do ?
my code :
<table height="200px" style="border:3px black solid;border-radius:5px" width="550px">
<tr>
<th colspan="2" height="40px" style="border-bottom:3px black solid">posts in this site</th>
</tr>
<?php
$link=mysql_connect("localhost","username","pass");
mysql_select_db("itstuff_paging",$link);
$q="select count(*) \"total\" from posts";
$ros=mysql_query($q,$link) or die(mysql_error());
$row=mysql_fetch_array($ros);
$total=$row['total'];
$dis=4;
$total_page=ceil($total/$dis);
$page_cur=(isset($_GET['page']))?$_GET['page']:1;
$k=($page_cur-1)*$dis;
$q="select * from posts limit $k,$dis";
$ros=mysql_query($q,$link);
while($row=mysql_fetch_array($ros))
{
echo '<tr>';
echo '<td width="10px" style="border-bottom:1px #a1a1a1 solid">'.$row
['post_id'].'.';
echo '<br/>';
echo '<td style="border-bottom:1px #a1a1a1 solid">'.$row['post_title'];
echo '<br/>';
echo ($row['post_content']);
echo '<br/>';
echo '<img src= images/$post_image />';
echo '</tr>';
}
echo '</table>';
echo '<br/>';
if($page_cur>1)
{
echo '<a href="index.php?page='.($page_cur-1).'"
style="cursor:pointer;color:green;" ><input style="cursor:pointer;background-
color:green;border:1px black solid;border-radius:5px;width:120px;height:30px;color:white;font-
size:15px;font-weight:bold;" type="button" value=" Previous "></a>';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-
radius:5px;width:120px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button"
value=" Previous ">';
}
for($i=1;$i<$total_page;$i++)
{
if($page_cur==$i)
{
echo ' <input style="background-color:green;border:2px black
solid;border-radius:5px;width:30px;height:30px;color:black;font-size:15px;font-weight:bold;"
type="button" value="'.$i.'"> ';
}
else
{
echo '<a href="index.php?page='.$i.'"> <input style="cursor:pointer;background-
color:green;border:1px black solid;border-radius:5px;width:30px;height:30px;color:white;font-
size:15px;font-weight:bold;" type="button" value="'.$i.'"> </a>';
}
}
if($page_cur<$total_page)
{
echo '<a href="index.php?page='.($page_cur+1).'"><input
style="cursor:pointer;background-color:green;border:1px black solid;border-
radius:5px;width:90px;height:30px;color:white;font-size:15px;font-weight:bold;" type="button"
value=" Next "></a>';
}
else
{
echo '<input style="background-color:green;border:1px black solid;border-
radius:5px;width:90px;height:30px;color:black;font-size:15px;font-weight:bold;" type="button"
value=" Next ">';
}
?>
</table>
You never define $post_image before you use it, so you're generating <img src="" />. Perhaps you meant:
echo "<img src=images/{$row['post_image']} />";
instead?
Your code is malformed. Please try:
echo '<img src="images/'.$post_image.'" />';

While loop output on the same line

I have a PHP loop where i output some stuff from my database to my page but i have a problem, when runing the code it ends up on different lines like example 1 When i want it to be on the came line line example 2, i have tried usiv div, floating the container and using CSS to make it inline but it just end up like example 1 all the time, why?
Example 1:
1
2
3
4
5
Examle 2:
1,2,3,4,5
<?php
$getquery=mysql_query("SELECT * FROM dogs ORDER by ID LIMIT 5");
while($rows=mysql_fetch_assoc($getquery))
{
$id=$rows['id'];
echo '<br>';
echo '<div id="block" border="1" width="200" style="float:center">';
echo '
<a href="mypage.com/index.php?dog=', $id, '">
<img src="/thumb/', $id, '.jpg" alt="dogs!" />
</a>';
echo '</div>';
}
?>
Take line break out of the loop. Each time there is a result, you print <br> that's why it's on new line
echo '<br />';
echo '<div id="block" border="1" width="200" style="float:center">';
$getquery=mysql_query("SELECT * FROM dogs ORDER by ID LIMIT 5");
while($rows=mysql_fetch_assoc($getquery)){
$id=$rows['id'];
echo '<a href="mypage.com/index.php?dog='. $id .'">
<img src="/thumb/'. $id .'.jpg" alt="dogs!" />
</a>';
}
echo '</div>';
and yes to the comments - please look into using mysqli
with counter:
echo '<div id="block" border="1" width="200" style="float:center">';
$i = 0;
$getquery=mysql_query("SELECT * FROM dogs ORDER by ID LIMIT 5");
while($rows=mysql_fetch_assoc($getquery)){
$id=$rows['id'];
echo '<a href="mypage.com/index.php?dog='. $id .'">
<img src="/thumb/'. $id .'.jpg" alt="dogs!" />
</a>';
$i++;
if($i == 5) {
echo '<br />';
$i = 0;
}
}
echo '</div>';
It looks like your concatenation is wrong.
<a href="mypage.com/index.php?dog='. $id . '">
<img src="/thumb/'. $id . '.jpg" alt="dogs!" />

What's wrong with this php-generated table?

I am trying to make a table via PHP, but when I load this, it displays it like this..
The code:
<table border="1" cellpadding="5">
<?php
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
?>
</table>
My goal is to display a table of 4 columns by 3 rows..
EDIT:
Found it already, it makes a new <tr> tag everytime the first while is performed.
The block :
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';
echo $test["count"];
echo 'x</td>';
};
will create 3 cells 1x, 1x, 2x first,
next the block:
while($row=mysql_fetch_array($topresult)) {
echo '<td width="150">';
echo $row["productnaam"];
echo '</td><td width="100" style="text-align:center;">€ ';
echo $row["prijs"];
echo '</td><td width="50" style="text-align:center;">';
echo '<a style="text-decoration:none;color:red;" href="#"><img width="25" src="trash.png"></a>';
echo '</td></tr>';
};
will create cells from Monitor, so the result become like this.
To fix it, you should save the result from the first while loop to an array and go through at the 2nd while loop
You are echoing nested rows.
while($test= mysql_fetch_assoc($countquery)){
echo '<tr><td>';// Here you open a row
echo $test["count"];
echo 'x</td>';//No closing of row, you close the td and open another tr on the next iteration
};

Display search results in two rows

I'm making a webpage and i add it a search engine. Here is the code from the results page.
$term = $_POST['term'];
$sql = mysql_query("select * from artists2 where Fname like '%$term%' or Genre like '%$term%' or Specialty like '%$term%' order by Fname");
while ($row = mysql_fetch_array($sql)){
echo '<table width="550" border="0" cellspacing="0" cellpadding="0">';
echo '<tr>';
echo '<td width="550" height="200"><img src="'.$row['Bio']. '" alt="" width="150" height="200"></td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30">Name: '.$row['Fname'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30">Genre: '.$row['Genre'].'</td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30">Specialty: '.$row['Specialty'].'</td>';
echo '</tr>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
echo '</table>';
}
?>
I want the results to be shown in two rows instead of one row which is shown now.
Also each result has a picture and 3 lines of text. I want the text to be show on the right side of the picture. Not below the picture as it shows now.
UPDATE 1
Dainis your code was very helpful. So close to what i need.
You code gives me this:
But i need something like, this:
Wow. That's some seriously outdated HTML code right there. You should never use tables for layout the way you're doing. There are much better ways of solving layouts like these nowadays.
Here's a JSFiddle which uses CSS for layout. The HTML in the fiddle is semantic meaning it accurately represents the data that you are displaying to your visitors (with or without sight) as well as search engines:
http://jsfiddle.net/CgbQH/
As you can see the list of search results is just that; a list. Each result has an image and a dl (some may argue dl isn't the right choice for key/value pairs though). CSS is then used to make the markup render the way you want.
Also, never ever stick user input (your $_POST['term']) directly into a query like that. Haven't you heard what happened to little Bobby Tables? http://xkcd.com/327/
Edit: I've used column-count to make the list render in two columns. column-count only works in modern browsers so if you have to support IE you can instead set each li to float: left; width: 50%. This will however render the items in a different order from column-count.
you need to remove some of your tags
while ($row = mysql_fetch_array($sql))
{
echo '<table width="550" border="0" cellspacing="0" cellpadding="0">';
echo '<tr>';
echo '<td width="550" height="200" valign="top"><img src="'.$row['Bio']. '" alt="" width="150" height="200"></td>';
echo '</tr>';
echo '<tr>';
echo '<td width="550" height="30" valign="top">';
echo 'Name: '.$row['Fname'].'<br />';
echo 'Genre: '.$row['Genre'].'<br />';
echo 'Specialty: '.$row['Specialty'];
echo '</td>';
echo '</tr>';
echo '</table>';
echo '<br/>';
echo '<br/>';
echo '<br/>';
}
How exactly do you want it to look like? Like this?
Then the code would be something like this:
while ($row = mysql_fetch_array($sql)){
echo '<table width="550" border="0" cellspacing="0" cellpadding="0" style="width: 40%; float: left;">';
echo '<tr>';
echo '<td width="550" height="200" align="left" valign="top"><img src="'.$row['Bio']. '" alt="" width="150" height="200"></td>';
echo '<td width="550" height="30" align="left" valign="top">';
echo '<p>Name: '.$row['Fname'].'</p>';
echo '<p>Genre: '.$row['Genre'].'</p>';
echo '<p>Specialty: '.$row['Specialty'].'</p>';
echo '</td>';
echo '</tr>';
echo '</table>';
}

Categories