displaying items from database in rows - php

This image shows my website's homepage.
http://i163.photobucket.com/albums/t315/smc22_2007/Website.png
I want rows of 4, not rows of 5.
Here is my php code:
<?php
$query = "SELECT * FROM UFPProducts";
$results = mysql_query ($query, $connect);
while ($row = # mysql_fetch_array($results))
{
print
"<div id= 'item'>" .
"<p>Product id ".$row["ProductID"]."</p>".
"<p><img src=".$row["Image"]."></p>".
"<p>&pound".$row["Price"]."</p>".
"<p>".$row["Description"]."</p>".
"</div>";
}
Here is the CSS for the above:
#item {
width:100px;
text-align:center;
border: 1px solid #D9D9D9;
padding: 0px;
list-style: none;
width: 150px;
float: left;
margin-right: 15px;
margin-bottom: 15px;
border-radius: 10px;
-moz-border-radius: 10px; /* firefox rounded corners */
-webkit-border-radius: 10px; /* Safari rounded corners */
min-height: 220px;
}
#item li h1 {
text-align:center:
}
#item li#white{
min-height: 220px;
}
How can I display rows of 4, not 5?
Thankyou

From looking at the code, there doesn't seem to be anything that "forces" five rows that you could simply change to four. It is quite possible that the width of the parent element naturally causes the items to appear in rows of five. If that is the case, you have a couple of options. You could add left and/or right margins or change the widths of the items to make them wider and force them to overflow to the next row sooner. Or, you could implement a counter in the PHP that would add a <br> tag (or similar) after every four items. You could also shrink the parent element, though that might not be an option. The CSS options are simple enough, in #item, increase the width:
#item {
width:100px;
/*... other css ...*/
}
In the PHP:
<?php
//rest of code
$counter=0;
while ($row = # mysql_fetch_array($results))
{
print
"<div id= 'item'>" .
"<p>Product id ".$row["ProductID"]."</p>".
"<p><img src=".$row["Image"]."></p>".
"<p>&pound".$row["Price"]."</p>".
"<p>".$row["Description"]."</p>".
"</div>";
$counter++;
if ($counter==3) {
$counter=0;
print "<br>";
}
}

Your products are currently set to float with a width of 150px. Since you're floating it will continue to add items until they don't fit (ie, if your items are 10px wide in a 75px wide container and you add 8, you'll see 7 on one 'row' with 1 on the next). You can simply increase the width to fit the container that your products are in until you've got 4 items per 'row'. Your margins and any padding also add to this so keep that in mind.
#item {
...
width: 200px;
...
}
If you don't want to increase the width of your product cells, then increase the margin to the increase the effective width of the elements.
#item {
...
margin-right: 20px;
...
}
You should also update your items to use a class instead of an ID since ID are supposed to be unique on a page.
.item {
...
}
<div class='item'>...</div>

As far as I see, while loop does not enforce any restrictions. Well my suggestion would be something like this:
$i = 1;
$cssClass = 'item';
$columns = 4;
while ($row = # mysql_fetch_array($results))
{
$class = $cssClass;
if (($i % $columns) == 0) {
$class .= ' clear';
}
print "<div id='{$class}'>" .
"<p>Product id " . $row["ProductID"] . "</p>" .
"<p><img src=" . $row[" Image"] . "></p>" .
"<p>&pound" . $row["Price"] . "</p>" .
"<p>" . $row["Description"] . "</p>" .
"</div>";
$i++;
}
And then in css define:
.clear { clear:both; }

Related

Dynamic Table with a horizontal Scrollbar in CSS

Working on a homepage I came to the following problem:
I want to display a table - the number of table columns can be chosen with checkboxes. Also, my website should work on a mobile phone, so I put in the correspondingly meta line.
Unfortunately, my website do not work properly as it do not shows my horizontal scrollbar..
PHP Code:
echo '<table id="dynamictable">';
echo '<tr>';
foreach($res_category_arr as $columnname)
{
echo '<th>' . $columnname. '</th>';
}
echo '</tr>';
if (mysqli_num_rows($result) > 0)
{
while($data_current_row=mysqli_fetch_assoc($result))
{
echo '<tr>';
foreach($res_category_arr as $tabledata)
{
echo '<td>' . $data_current_row[$tabledata] . '</td>';
}
echo '</tr>';
}
}
echo '</table>';
res_category_arr represents an array, which contains the wanted column names.
CSS Code:
#dynamictable{
width: 100%;
overflow-y: auto;
margin: 0 0 1em;
text-align: center;
}
#dynamictable::-webkit-scrollbar {
-webkit-appearance: none;
width: 14px;
height: 14px;
}
#dynamictable::-webkit-scrollbar-thumb {
border-radius: 8px;
border: 3px solid #fff;
background-color: rgba(0, 0, 0, .3);
}
Take the ID off the table, and put it on a div AROUND the table.
It should do what you're looking for, but you might also want to add a width:100% back to the table as well.
EDIT: Figured I would add a simple working example here.
http://codepen.io/anon/pen/qaGARr?editors=1100#0
.scroll {
overflow-x:auto;
width:100%;
}
table {
width:100%;
min-width:700px;
}
If you go below 700px wide, the scrollbar shows up

How do I place text in table with css and how to fix footer

What I want to do:
I want in each box, to be shown some data from database.
I've made a table which display it in 2 rows.. but I have some problems with CSS I guess.
My second problem is the footer. I want to make it so that it doesn't overlap the table when more results from the database are shown, but still don't know how.
I try with and without absolute position, etc but it's still mess.
CSS
#infobox h1 {
color: brown;
left: 90px;
top: 50px;
font-size: 13px;
}
#infobox p {
color: brown;
left: 160px;
top: 95px;
font-size: 13px;
}
=====================================
PHP
<table style ="position:absolute; left:0px">
<tbody>
<?php
$i = 1;
$columns = 2;
$data1 = mysql_query("SELECT * FROM `stuff` WHERE categorie='1' AND nr_redeems='0' ") or die(mysql_error());
while($row1 = mysql_fetch_array($data1))
{
if($i == 1){ echo '<tr>'; }
echo "<td id='infobox'>" . "<div class=''></div>"
. "<img style='position:relative; top:0px; left:0px; width:423px; height:121px' src='../img/button.gif'>"
. "<h1>Some data about it.</h1>"
. "<p><b>Price:"
. $row1['nr_points']
. "points</b></p>"
. "</td>";
$i++;
if($i == ($columns + 1)) { echo '</tr>'; $i = 1; }
}
?>
</tbody>
</table>
=====================================

Div is not rigged onto image and text is not centered properly?

I have a main Div which is the "master" of all divs. This div's width is 70% of the page. Inside this div, is another div that contains an image. This div is called mainImg. It is set to absolute positioning and all the rest of the elements are set to absolute as well. All these elements move absolutely relative to the main div.
Here's my code:
PHP/HTML
<?php
$resultSet = $db->query("SELECT * FROM Articles");
if ($resultSet->num_rows != 0)
{
while ($rows = $resultSet->fetch_assoc())
{
$id = $rows["id"];
if ($id <= 3)
{
$images = $rows["image"];
$title = $rows["title"];
echo "<div id=main>";
if ($id == 1)
{
echo "<div id=mainImg>";
echo "<img src=$images>";
echo "<div id=mainTitle>";
echo "<h2>$title</h2>";
echo "</div>";
echo "</div>";
}
echo "</div>";
}
}
}?>
CSS
body{
position: relative;
}
#main{
position: relative;
width: 70%;
height: auto;
margin: 1.5% auto;
}
#mainImg{
position: absolute;
width: 65%;
}
#mainImg img{
width: 100%;
}
#mainTitle{
position: absolute;
width: 100%;
height: 25%;
bottom: 0%;
background-color: rgba(100, 0, 0, 0.7);
}
#mainTitle h2{
color: white;
text-align: center;
font-family: sans-serif;
font-size: 120%;
}
My problem that I am facing is that the div inside of mainImg (mainTitle), is not properly rigged inside the image. The div is a semi-transparent block that should fit perfectly on the bottom of the image with 25% height. Instead, the block is coming out of the image a bit. The other problem that I am facing is my text inside this mainTitle div. The text is centered, but not aligned in the middle of the div. I am trying to make the text responsive, with percentages, but whenever I resize the text always goes below the div. How do I fix these three problems? (Rigging the div properly, aligning the text, and keeping the text inside the div at all times for other window sizes?
The semi-transparent div is coming off the edge of the image
When browser is resized, this is what happens to the text
<?php
$res = $db->query("SELECT * FROM Articles");
if( $res->num_rows > 0 ) {
echo "<div id='main'>";/* Open main div outside the loop */
while( $rows = $res->fetch_object() ) {
$id = $rows->id;
if ( $id <= 3 ) {
$image = $rows->image;
$title = $rows->title;
if ( $id == 1 ) {
echo "
<div id='mainImg'>
<img src='$image'>
<div id='mainTitle'>
<h2>$title</h2>
</div>
</div>";
}
}
}
echo '</div>';/* close main div */
}
?>

Need to display cell item from a loop horizontally

Trying to create a broad sheet for a school such that it loops the result database and fetches a student records and display it horizontally.
css controlling the style for the ul
#my-list{
background: #f7f7f;
padding: 8px; /* Give the items some air to breathe */
}
#my-list > li {
display: inline-block;
zoom:1;
*display:inline;
background:#99FFFF;
/* this fix is needed for IE7- */
}
#my-list > li > a{
color: #666666;
text-decoration: none;
padding: 3px 8px;
}
#my-list > li > a:hover{
color: black;
}
It starts the loop here for the scores. My problem is that I want to be able to display all the scores on one row but it breaks into a new line after each subject is displayed. I want it on a row eg: Biology ca(10) exam(70) total(100) Economics ca(10) exam(70) total(100), till the loop is completed but it keeps breaking into a new line.
do{
echo"<li>";
echo "<table width='200' border='1'>";
echo "<tr>";
echo "<td>".$dbfieldscore['subject_name']."</td>";
echo "<td>".$dbfieldscore['CA']."</td>";
echo "<td>".$dbfieldscore['exam']."</td>";
echo "<td>".$dbfieldscore['total']."</td>";
echo "</tr>";
echo "</table>";
echo "</li>";
} while($dbfieldscore=mysql_fetch_assoc($resultscore));
echo "<ul>";
echo "</td>";
It loops the database for student score. Here is the problem, I want to display the output horizontally but it goes to the next line.
Add white-space:nowrap; to the #my-list rule
#my-list{
background: #f7f7f;
padding: 8px; /* Give the items some air to breathe */
white-space:nowrap;
}
and to leave the inner contents unaffected you can add
#my-list > li {white-space:normal;}
Replace with this, add the spaces to visualize the output in a proper way as per your need.
echo "<td>";
do{
echo $dbfieldscore['subject_name']." ";
echo "CA: ".$dbfieldscore['CA'];
echo "Exam: ".$dbfieldscore['exam'];
echo "Total: ".$dbfieldscore['total'];
} while($dbfieldscore=mysql_fetch_assoc($resultscore));
echo "</td>";

How To Wrap Content That Is Generated Programatically In PHP Through Divs Depending On Screen Res

I have been racking my brains trying to figure out how to change what I have into something that will organise my content into as many rows as necessary and as many columns (divs) as necessary when the screen resolution changes.
Basically, my current website set up has a content div with a height of 430px which can accommodate any number of columns because I have a horizontal scroll. Within each column there can be up to 10 rows of data before another column is created for the next lot of data.
What I want to do is change the height of the content div to % not px so it will scale with the browser res. My current script will still only output the same number of rows and columns if the content div is too small of too big, so what I want is to have the number of rows less for smaller screen res's and more for higher res screens.
I kind of want the content to wrap into the next column (div) if the browser is made smaller.
Hope this makes sense.
Can someone point me in the right direction? I know this code is currently written differently but I cant think how to do it:
functions.php:
<?php
function printTranscriptions()
{
global $link;
$query = "SELECT * FROM transcriptions ORDER BY artist, title";
if ($result = mysqli_query($link, $query)) {
$count = 0;
while ($row = mysqli_fetch_row($result)) {
if (($count % 10) == 0) {
if ($count > 0) {
echo "</div>\n";
}
echo '<div class="content_columns">'."\n";
}
$artistTitle = $row[1] . ' - ' . $row[2];
echo '<p>' . $artistTitle . '</p>'."\n";
$count++;
}
if ($count > 0) {
echo "</div>\n";
}
/* free result set */
mysqli_free_result($result);
}
}
My index file calls the content in the following structure:
<!-- Transcriptions page start -->
<div class="transcriptions_page">
<h1>
Transcriptions
</h1>
<div class="content">
<div class="list-wrapper">
<div class="transcription-list">
<?php printTranscriptions();?>
</div>
<!-- transcription-list end -->
</div>
<!-- list-wrapper end -->
</div>
<!-- content end -->
</div>
<!-- Transcriptions page end -->
My css:
.content {
display: block;
overflow: hidden;
background-color: #2D949E;
color: #fff;
text-shadow: #FAFAFA;
margin-top: 75px;
margin-left: 10%;
margin-right: 10%;
padding: 20px 20px 20px 20px;
height: 430px;
border: 1px solid #99E9F1;
}
.list-wrapper {
overflow: hidden;
}
.transcription-list {
display: block;
white-space: nowrap;
}
.content_columns {
display: inline-block;
background-color: #429EA8;
letter-spacing: 1px;
margin-right: 20px;
padding: 0 10px;
}
.content_columns:last-child {
margin-right: 0 !important;
}
Thanks

Categories