make records fetched from database as responsive - php

I'm using bootstrap framework.
i have code like this
<?php
$id=$_GET["pid"];
$query="Select * FROM product WHERE pid='$id'";
$result=mysql_query($query) or trigger_error(mysql_error());
while ($row=mysql_fetch_array($result)) {
$image=$row['image']; --->image path
$head=$row['head'];
?>
<div class="col-md-12">
<ul class="cgrid">
<li>
<a href=<?php echo "\" page.php?pid=$id \"" ;?>><?php echo $image;?>
<h4><?php echo $head ;?></h4>
</a>
</li>
</ul>
</div>
<?php
}
?>
.cgrid li {width: -webkit-calc(100% / 3);}
I have included responsive CDN as well.
Let's say i have 6 records in my database that fetches as 3 records per row.
What i want is:I want the records to be responsive ie.,i don't want it to stretch ,i want it to align it to 2 or 1 for a row as the screen size decreases. how can i achieve this ?Thanks in advance :)

Related

How to insert echo into div

I'm new here, so do not be angry if I ask something that is already answered.
I connected sql database:
connect.php
<?php
$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("mobilni", $connect) or die(mysql_error());
?>
displaydata.php
<?php
include "connection.php";
$sql= "SELECT * FROM imena WHERE Okrug='Beogradski'";
$query=mysql_query($sql) or die (mysql_error());
?>
<div class="beyondheader"></div>
<div class="header">
<div id='cssmenu'>
<ul>
<li class='active'><a href='index.html'>Početna</a></li>
<li><a href='proizvodjaci.html'>Proizvodjači</a></li>
<li><a href='oglasi.html'>Oglasi</a></li>
<li><a href='about.html'>O nama</a></li>
</ul>
</div>
</div>
<div class="middle">
<div class="leftmiddle">ss
</div>
<div class="rightmiddle">
<?php
while ($row = mysql_fetch_array($query)){
?>
<div class="divmobilni">
Ime:<div class="mobilniime"><?php echo $row['Ime'];?></div>
Okrug:<?php echo $row['Okrug'];?>
</div>
<?php } ?>
</div>
</div>
And everything is working fine. Now I want to put every result in the other <div> automatically?
How to do that?
Sorry if I wasn't clear enough. I want to sort results to be like this, in one <div> goes: Ime: Okrug: [that is data for one person]
Now, I want to make that all data from my SQL table display on this page, but every Person separately from this <div>.
To be something like this, with data from table: (This is just example drawn in Paint)
I fixed this with adding only break to the end of PHP. Thanks in any case!
Exactly the same as you would with php open and close <?php ?>
Use this inside the divs you would like or just use echo with the div inside

PHP - display database values on every content box in PHP

<li class="list-group-item">
<a href="/user/Michael" class="thumb-sm pull-left m-r-sm">
<img src="http://www.gravatar.com/avatar/8b7a9ba3cbf958009080f6da12a55029?&d=mm&r=g?&d=mm&r=g&s=215" class="img-circle">
</a>
<a href="user/Michael" class="clear">
<strong class="block">
<?php include '/includes/connection.php';?>
<?php echo $products['Title'] ; ?>
</strong>
<?php include '/includes/connection.php';?>
<small><?php echo $products['Followers'] ; ?> Followers </small>
</a>
</li>
<li class="list-group-item">
<a href="/user/Steven" class="thumb-sm pull-left m-r-sm">
<img src="http://www.gravatar.com/avatar/a5fb2decd550cdf33cbb8ce7566ba772?&d=mm&r=g?&d=mm&r=g&s=215" class="img-circle">
</a>
<a href="/user/Steven" class="clear">
<strong class="block">
<?php include '/includes/connection.php';?>
<?php echo $products['Title'] ; ?>
</strong>
<small><?php echo $products['Followers'] ; ?> Followers</small>
</a>
</li>
do i need to manually insert for every content?
I have over 100 contents how can i automatically insert in every line like content 1 display row 1 followers and content box 2 display row 2 followers and so on
The trick here is to make a loop within the code. This means you have to generate all content from out of the database or it's not gonna work. Let me show you an example:
<ul>
<?php
$sql = "SELECT ProjectId, ProjectTitel, ProjectExpertise
FROM project";
$stmt1 = mysqli_prepare($con, $sql);
mysqli_stmt_execute($stmt1);
mysqli_stmt_bind_result($stmt1,$ProjectId,$ProjectTitel,$ProjectExpertise);
while (mysqli_stmt_fetch($stmt1)){
?>
<li class="wow fadeInLeft" data-wow-offset="30" data-wow-duration="1.5s" data-wow-delay="0.15s">
<a href="inc/elements/project.php?id=<?php echo $ProjectId; ?>" class="meer">
<img src="img/portfolio/<?php echo $ProjectId; ?>/thumbnail/1.jpg" alt="<?php echo $ProjectTitel; ?> project">
<div class="project-info">
<div class="project-details">
<h5 class="witte-text blauwe-streep-onder">
<?php echo $ProjectTitel; ?>
</h5>
<div class="details witte-text">
<?php echo $ProjectExpertise; ?>
</div>
</div>
</div>
</a>
</li>
<?php
}
?>
</ul>
I start an UL outside of the PHP code and then I start my PHP query, as you see, i select the items i need from the database. Here i create a while loop and run through my setup of the list element. As you see, i use my items within the project id link (the link is an ajax call to another page), the thumbnail needed to show the picture, the title and the expertise i used.
In your case, the while loop you need requires more than just the the title and followers. I think you need as well an userid / username so you can take in account which user it is. Your loop would now proces all on the same user, since its staticly defined in your code. Also the avatar picture is staticly defined. Let me try to resolve some for you.
<?php
include '/includes/connection.php';
$sql = "SELECT products.title, products.followers
FROM products"; // as example query
$stmt1 = mysqli_prepare($con, $sql);
mysqli_stmt_execute($stmt1);
mysqli_stmt_bind_result($stmt1,$title, $followers);
while (mysqli_stmt_fetch($stmt1)){
?>
<li class="list-group-item">
<a href="/user/Michael" class="thumb-sm pull-left m-r-sm"> <!-- make the username also to be pulled out of a database. -->
<img src="http://www.gravatar.com/avatar/8b7a9ba3cbf958009080f6da12a55029?&d=mm&r=g?&d=mm&r=g&s=215" class="img-circle">
</a> <!-- you should save the avatar link into a database as well -->
<a href="user/Michael" class="clear"> <!-- make the username also to be pulled out of a database. -->
<strong class="block">
<?php echo $title; ?>
</strong>
<small>
<?php echo $followers; ?>
Followers
</small>
</a>
</li>
<?php
}
?>
This as example. As you see, i added notitions behind some code, to explain this should also be dynamic, since it will help you Remember. being a programmer is to find shortcuts on how you display your information. Code that repeats itself constantly with a changing variable should always be looped, to make sure you dont type unneccesairy code.
Since I dont know how your database is made, i can only guess, Michaels name is probably linked to an ID in the same table your products are, which you can use then in to pull out of the database, by searching in your user table. I hope I make sense here. For instance, your products.userid should be as well in your user table as user.userid. Most likely the userid will have a name linked to it in the user table.
$sql = "SELECT products.title, products.followers, products.userid, user.userid, user.username
FROM products, user
WHERE user.userid = product.userid";
So now you have in each row as well the name of the person of who's title it is. And that you can echo out again in the code i put up. (make sure you bind the result in the same order as you pulled them up)
Writing code is all about making it easier to display your information. Loops is and stays the keyword here, as NadirDev explains.
I hope I helped you getting on the right track.

Loop through PHP function, display userdata from Mysql - limit to display to 25 items and add next button

I have this PHP function, which I use to display my userdata. If run once it will basicly display a div with the userdata in it.
function listings($fornavn, $efternavn, $email, $adresse, $tlf, $postnr, $city, $fodselsdag, $brugerid,$bartype,$idbar)
{
?>
<div class="container">
<span class="records" id="id-<?php echo $brugerid;?>">
<div class="customer bidDiv clearfix">
<?php if ($bartype=='temp_vip') { ?>
<ul>
<li>
<span class="actionSpan" id="<?php echo $brugerid;?>" value="<?php echo $idbar;?>">
<a class="edit-opt" id="godkend" href="#">GODKEND</a>
</span>
</li>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Afvis</a>
</span>
</li>
<?php }else{ ?>
<ul>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Slet</a>
</span>
</li>
<li>
<a class="edit-opt" href="editform.php?id=<?php echo $brugerid."&bartype=".$bartype;?>" rel="facebox">Rediger</a>
</li>
<?php if ($bartype =='vip'){?>
<li>
<a class="print-opt" href="print.php?id=<?php echo $brugerid;?>" rel="facebox">Print</a>
</li>
<?php }else{
// Dont render vip link
}}?>
</ul>
<p class="contact-data">
<?php echo $email;?><br>
Tlf.: <?php echo $tlf;?>
</p>
<div class="base-data">
<h4><?php echo ucwords($fornavn)." ".ucwords($efternavn);?></h4>
<p>Fødselsdag <?php echo $fodselsdag;?></p>
<address><?php echo ucwords($adresse) ." ". $postnr ." ". ucwords($city);?></address>
</div>
</div><!-- end customer -->
</div>
</span>
<?php
I mostly use this function in a loop, to display all users from my MySQL database, who has a specific relation to something else.
This is all working great. But currently it would pull all results at once. Right now I only have 5 members, so its no problem, but say I get 800 or 2000 members, the list of users would get very long.
So I want to implement some kind of limit on how many users it displays, but still be able to browse through all the users. Many sites use something like pages and split up the results that way. Links like:
[1] [2] [3] [Last page>>]
I can't figure out how to start doing this? How would I proceed?
Also looked into this Jquery plugin:
http://andersonferminiano.com/jqueryscrollpagination/
But it keeps reloading my PHP file which results in the Loops being restarted, and it simply displays the results over and over again endlessly.
Can someone help me with the logic behind creating this? Or better, point me in a direction where I could use the jquery pluging above - where it only loads the loop one time, and renders the results as I scroll.
Any help or pointers will be greatly appreciated!
Thanks
Jquery pagination by default will only break-up the given table into several tabs on the page, that is all the data is actually loaded. There are some that let you do an AJAX request to fetch the next page, to work in conjunction with your PHP code.
Ideally, you want to limit the query using LIMIT (number of rows in the result object) and OFFSET (Start from row X ) which will give you number of records starting from your offset row up until the limit, and then use logic on the PHP side to determine the maximum number of pages.
$page = $_POST["page"] * 25; // make sure that page 0 is the starting page or calculate
$SQL = "SomeQueryText LIMIT 25 OFFSET '$page'" ;
$result = query ($SQL);

Get image from portfolio page and post in homepage

Hi I have created myself a portfolio website.On the homepage in the footer on the right I would like to add four images from the portfolio page that change every time the page is loaded.Can anyone tell me how this can be achived?
On the portfolio page this is the structure for the html:
<ul class="ourHolder">
<li class="item" data-id="id-1" data-type="websites" >
<img src="img/portfolio/websites/small/1.jpg" alt="">
<div class="full"></div>
</li>
</li>
<li class="item" data-id="id-2" data-type="pdesigns">
<img src="img/portfolio/Print/small/1.jpg" alt="" />
<div class="full"></div>
</li>
<li class="item" data-id="id-3" data-type="ldesign">
<img src="img/portfolio/Logo/small/001.jpg" alt="" />
<div class="full"></div>
</li>
....
</ul>
I am using the jquery quicksand plugin to create a nice effect.I am trying to randomnly get 4 images from this page and post them on the homepage in the footer.Here is my website:
foxteam.net
Based on the information you provided, here's what you can do
//Database connection here
$sql="
SELECT image_link FROM photos
ORDER BY RAND()
LIMIT 4";
//Executing the query here
while($row = $result->fetch()){
echo '<img src="', $row['image_link'], '" />';
}
Edit: Please note that ORDER BY RAND() has some performance issues with big tables, if your "gallery" table is under 100, then I don't think you should worry about it.

Getting Duplicates from phpMyAdmin Database Table

Will someone PLEASE show me what I'm doing wrong? I'm new to using phpMyAdmin and db.
I have a db table with 4 items. Looks fine on a plain php page - I get all four items in correct order.
But if I use coding <ul class="column"><li><div class="imgblock"> like shown below, I get 4 duplicates of my last entry in the db table. I think I have to rearrange how I use ", ', and ), etc.? Not sure how though...
<?php
// Make a MySQL Connection
mysql_connect("localhost", "....", "....") or die(mysql_error());
mysql_select_db("....") or die(mysql_error());
$show = "SELECT pn, pgname, img, name, price FROM prodshort";
$result = mysql_query ($show);
while ($show = mysql_fetch_array ($result))
{
$field2= $show['pn'];
$field3= $show['pgname'];
$field4= $show['img'];
$field5= $show['name'];
$field6= $show['price'];
$field7= $show['specs'];
}
?>
<!-- start -->
<ul class="column"><li><div class="imgblock">
<a href="<?echo "$field3";?>">
<img src="<?echo "$field4";?>" width="320" height="240" alt="<?echo "$field5";?>" /></a></div><br />
<?echo "$field5";?>
<ul class="specs">
<?echo "$field7";?></ul>
<div class="price">
#<?echo "$field2";?> $<?echo "$field6";?>
</div></li></ul>
<!-- end -->
The above is probably not the code from the example, as the while loop is outside your items.
It should be more like this:
while ($show = mysql_fetch_array ($result)) {
$field2= $show['pn'];
$field3= $show['pgname'];
$field4= $show['img'];
$field5= $show['name'];
$field6= $show['price'];
$field7= $show['specs'];
?>
<!-- start -->
<ul class="column">
<li>
<div class="imgblock">
<a href="<?echo "$field3";?>">
<img src="<?echo "$field4";?>" width="320" height="240" alt="<?echo "$field5";?>" />
</a>
</div>
<br />
<?echo "$field5";?>
<ul class="specs">
<?echo "$field7";?>
</ul>
<div class="price">
#<?echo "$field2";?> $<?echo "$field6";?>
</div>
</li>
</ul>
<!-- end -->
<?
// end of while loop
}
?>
(You might find it easier to debug if you indent your code correctly, most editors do that for free ;-)

Categories