How to echo specific elements from a database - php

I'm trying to echo certain elements from my news table throughout an article. This involves fields such as the author, headline, content and date. At the moment I can only echo the row and any attempt to echo elements in the page itself is either met with small errors or it outputs nothing. I've had a look around and only found people having problems with simply printing out the row database which I can already do.
Basically when I have Author in my article I want to echo author from my database and it will show next to Author. I'm not sure how possible it is as I am yet to find anything on this or I'm overlooking it.
Here is my current PHP file and I've left in the initial part with the article author, date and headline.
<?php
/* Database connection settings */
$host = 'xxxxx';
$user = 'xxxxx';
$pass = 'xxxxx';
$db = 'xxxxx';
$dbconnect=mysqli_connect($host,$user,$pass,$db);
if ($dbconnect->connect_error) {
die("Database connection failed: " . $dbconnect->connect_error);
}
$query = mysqli_query($dbconnect, "SELECT * FROM news WHERE news_Id = 1")
or die (mysqli_errr($dbconnect));
while ($row = mysqli_fetch_array($query)) {
echo
"<tr>
<td>{$row['headline']}</td>
<td>{$row['content']}</td>
<td>{$row['author']}</td>
<td>{$row['date']}</td>
</tr>\n";
}
?>
<div class="container-fluid bg">
<div class="row">
<div class="col-md-1"></div>
<div class="col-md-10">
<div class="container">
<div class="row mb-2">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-md-12">
<div class="news-title">
<h2><?php echo $row['headline']; ?></h2>
</div>
<div class="news-cats">
<ul class="list-unstyled list-inline mb-1">
<li class="list-inline-item">
<i class="fa fa-folder-o text-danger"></i>
<small>Author:</small>
</li>
<li class="list-inline-item">
<i class="fa fa-folder-o text-danger"></i>
<small>Posted:</small>
</li>
</ul>
</div>
<hr>

Since your query only returns one row, a while loop is inappropriate for fetching the data, as at the end of the loop, $row will be left as a false value, thus making any attempt to access e.g. $row['headline'] fail. Change your while loop
while ($row = mysqli_fetch_array($query)) {
to a simple assignment:
$row = mysqli_fetch_array($query) or die(mysqli_error($dbconnect));
Note you have a typo earlier in your code,
or die (mysqli_errr($dbconnect));
should be
or die (mysqli_error($dbconnect));

You are just printing out the result, but don't have a label. You can add one like this:
while ($row = mysqli_fetch_array($query)) {
echo
"<tr>
<td>Headline: {$row['headline']}</td>
<td>Content: {$row['content']}</td>
<td>Author: {$row['author']}</td>
<td>Date: {$row['date']}</td>
</tr>\n";
}

Related

Bootstrap cards stacking vertically

Afternoon all, I'm currently working on a personal project using the bootstrap framework and PHP. Essential what I am trying to achieve is when I pull data from my MySQL database for it to be displayed in the bootstrap card element in horizontal rows of 4 individual cards.
As it stands the technical aspects of pulling data and displaying it works. But when going onto the page, the card elements are stacked. On top of each other vertically in one long list. As it stands, I'm not too sure how to fix this any help would be appreciated.
What it looks like atm
Main Page Code
<?php
include("inc/dbconfig.php");
include("inc/functions.php");
error_reporting(0);
$search = $_POST['search'];
$mysqlQ =("SELECT * FROM cards WHERE cardName LIKE '%$search%'");
$result = $conn -> query($mysqlQ);
if ($result -> num_rows > 0) {
while ($row = $result -> fetch_assoc()) {
itemCard($row);
}
}
else {
echo "<h1 class='search-h1'>No Results Found!</h1>";
echo "<h4 class='search-h4'>It looks like no results could be found, please try again.</h4>";
}
$conn -> close();
Card Function
<?php function itemCard (array $row) { ?>
<div class="container">
<div class="row">
<div class="col-lg-3 col-md-4 col-sm6 col-xs-12">
<div class="card">
<div class="card-img">
</div>
<div class="card-body">
<h5 class="card-title text-center"><?= $row["cardName"] ?></h5>
<p class="text-center">Card Set: <b><?= $row["cardSet"] ?></b></p>
<p class="text-center">Card Set: <b><?= $row["cardRarity"] ?></b></p>
</div>
</div>
</div>
</div>
</div>
<?php } ?>
Note: Currently not using any custom CSS at this stage I was trying to use the default bootstrap functionality for the layout
You are returning a container and a row on each iteration.
You should have a single container and a row generated after 4 cards.
You should print your container and row once before printing items.
Main Page Code
<?php
include("inc/dbconfig.php");
include("inc/functions.php");
error_reporting(0);
$search = $_POST['search'];
$mysqlQ =("SELECT * FROM cards WHERE cardName LIKE '%$search%'");
$result = $conn -> query($mysqlQ);
if ($result -> num_rows > 0) {
# container beginning tags
echo '<div class="container"><div class="row">';
# container content
while ($row = $result -> fetch_assoc()) {
itemCard($row);
}
# container ending tags
echo '</div></div>';
}
else {
echo "<h1 class='search-h1'>No Results Found!</h1>";
echo "<h4 class='search-h4'>It looks like no results could be found, please try again.</h4>";
}
$conn -> close();
Card Function
<?php function itemCard (array $row) { ?>
<div class="col-lg-3 col-md-4 col-sm6 col-xs-12">
<div class="card">
<div class="card-img">
</div>
<div class="card-body">
<h5 class="card-title text-center"><?= $row["cardName"] ?></h5>
<p class="text-center">Card Set: <b><?= $row["cardSet"] ?></b></p>
<p class="text-center">Card Set: <b><?= $row["cardRarity"] ?></b></p>
</div>
</div>
</div>
<?php } ?>

PHP Pagination not working when Records Filter is applied

I have written a PHP script which has option to filter records and display filtered records pagewise.
My Problem:
When I apply the filter, the records are displayed but when I click on any page number it displays all records page wise instead of showing filtered records pagewise.
How can I correct this?
Here is my complete script,
<body>
<form style="background-color:darkorange; font-size: 13px;" id="form1" name="form1" method="post" action="displaydesign.php">
<label>Design Category</label>
<select name="dlocation">
<option value="">All</option>
<?php
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." GROUP BY dlocation ORDER BY dlocation";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
while ($row = mysql_fetch_assoc($sql_result)) {
echo "<option value='".$row["dlocation"]."'".($row["dlocation"]==$_REQUEST["dlocation"] ? " selected" : "").">".$row["dlocation"]."</option>";
}
?>
</select>
<input type="text" name="string" id="string" value="<?php echo stripcslashes($_REQUEST["string"]); ?>" placeholder="Search by Name or City" />
<input type="submit" name="button" id="button" value="Filter" />
<a style="background-color:white;" href="displaydesign.php"> Reset</a>
</form>
<hr>
<?php
if ($_REQUEST["string"]<>'') {
$search_string = " AND (dname LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%' OR dcity LIKE '%".mysql_real_escape_string($_REQUEST["string"])."%')";
}
if ($_REQUEST["dlocation"]<>'') {
$search_dlocation = " AND dlocation='".mysql_real_escape_string($_REQUEST["dlocation"])."'";
}
$per_page=2; // no.of records per page
if (isset($_GET["page"])) {
$page = $_GET["page"];
}
else {
$page=1;
}
// Page will start from 0 and Multiple by Per Page
$start_from = ($page-1) * $per_page;
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE did>0".$search_string.$search_dlocation." order by did desc LIMIT $start_from, $per_page";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
if (mysql_num_rows($sql_result)>0) {?>
<div class="row">
<?php while ($row = mysql_fetch_assoc($sql_result)) { ?>
<div class="col-sm-6">
<div class="card" >
<h3 class="card-header card-success text-center"><?php echo $row['dlocation'] ?></h3>
<img class="card-img-top img-fluid" src="<?php echo $row['dimage'] ?>" alt="Card image cap">
<div class="card-block ">
<h4><span class="badge badge-default">Designer Information</span></h4>
<h5 class="card-title"><?php echo $row['dname'] ?></h5>
<h6 class="card-subtitle mb-2 text-muted "><?php echo ($row['dcity'])?></h6>
<p class="text-right">
<a class="btn btn-danger btn-sm " data-toggle="collapse" href="#collapseExample<?php echo ($row['did'])?>" aria-expanded="false" aria-controls="collapseExample<?php echo ($row['did'])?>">
Know More..
</a>
</p>
<div class="collapse" id="collapseExample<?php echo ($row['did'])?>">
<div class="card card-block">
<h5><span class="badge badge-warning">Contact Info</span></h5>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['demail'])?></h6>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['dmobile'])?></h6>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['daddress'])?></h6>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['dcity'])?></h6>
<h6 class="card-subtitle mb-2 text-muted"><?php echo ($row['dwebsite'])?></h6>
<!--ACCORDION START-->
<div id="accordion<?php echo ($row['did'])?>" role="tablist" aria-multiselectable="true">
<div class="card">
<div class="card-header" role="tab" id="headingOne<?php echo ($row['did'])?>">
<h5 class="mb-0 btn-sm">
<a data-toggle="collapse" data-parent="#accordion<?php echo ($row['did'])?>" href="#collapseOne<?php echo ($row['did'])?>" aria-expanded="true" aria-controls="collapseOne<?php echo ($row['did'])?>">
Image Description
</a>
</h5>
</div>
<div id="collapseOne<?php echo ($row['did'])?>" class="collapse show" role="tabpanel" aria-labelledby="headingOne<?php echo ($row['did'])?>">
<div class="card-block">
<?php echo $row['dimagedescription'] ?>
</div>
</div>
</div>
<div class="card">
<div class="card-header" role="tab" id="headingTwo<?php echo ($row['did'])?>">
<h5 class="mb-0 btn-sm">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion<?php echo ($row['did'])?>" href="#collapseTwo<?php echo ($row['did'])?>" aria-expanded="false" aria-controls="collapseTwo<?php echo ($row['did'])?>">
Software Used
</a>
</h5>
</div>
<div id="collapseTwo<?php echo ($row['did'])?>" class="collapse" role="tabpanel" aria-labelledby="headingTwo<?php echo ($row['did'])?>">
<div class="card-block">
<p class="card-text"><?php echo $row['dsoftwareused'] ?></p>
</div>
</div>
</div>
<div class="card">
<div class="card-header" role="tab" id="headingThree<?php echo ($row['did'])?>">
<h5 class="mb-0 btn-sm">
<a class="collapsed" data-toggle="collapse" data-parent="#accordion<?php echo ($row['did'])?>" href="#collapseThree<?php echo ($row['did'])?>" aria-expanded="false" aria-controls="collapseThree<?php echo ($row['did'])?>">
My Brand Recommendation
</a>
</h5>
</div>
<div id="collapseThree<?php echo ($row['did'])?>" class="collapse" role="tabpanel" aria-labelledby="headingThree<?php echo ($row['did'])?>">
<div class="card-block">
<div class="card">
<ul class="list-group list-group-flush">
<li class="list-group-item"><?php echo $row['dbrandname'] ?></li>
<li class="list-group-item"><?php echo $row['dbrandsegment'] ?></li>
<li class="list-group-item"><?php echo $row['dbrandwebsite'] ?></li>
<li class="list-group-item"><?php echo $row['dbrandemail'] ?></li>
</ul>
</div>
</div>
</div>
</div>
</div>
<!--ACCORDION END-->
</div>
</div>
</div>
<div class="card-footer">
<small class="text-muted">Design ID:- <?php echo stripcslashes($row['did']) ?> Submitted on :-<?php echo stripcslashes($row['dsubmissiondate']) ?></small>
<br>
</div>
</div>
</div>
<?php } ?>
</div>
<?php } else { ?>
<h3>No results found for the desired Search.</h3>
<?php } ?>
<!-- jQuery first, then Tether, then Bootstrap JS. -->
<script src="https://code.jquery.com/jquery-3.1.1.slim.min.js" integrity="sha384-A7FZj7v+d/sdmMqp/nOQwliLvUsJfDHW+k9Omg/a/EheAdgtzNs3hpfag6Ed950n" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/tether/1.4.0/js/tether.min.js" integrity="sha384-DztdAPBWPRXSA/3eYEEUWrWCy7G5KFbe8fFjk5JAIxUYHKkDx6Qin1DkWx51bBrb" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/js/bootstrap.min.js" integrity="sha384-vBWWzlZJ8ea9aCX4pEW3rVHjgjt7zpkNpZk+02D9phzyeVkE+jo0ieGizqPLForn" crossorigin="anonymous"></script>
<div>
<?php //for page numbers display at bottom of page
//Now select all from table for pagination
$sql = "SELECT * FROM ".$SETTINGS["data_table"]." WHERE did>0".$search_string.$search_dlocation." order by did desc";
$sql_result = mysql_query ($sql, $connection ) or die ('request "Could not execute SQL query" '.$sql);
// Count the total records
$total_records = mysql_num_rows($sql_result);
//Using ceil function to divide the total records on per page
$total_pages = ceil($total_records / $per_page);
//Going to first page
echo "<center><a href='displaydesign.php?page=1'>".'First Page'."</a> ";
for ($i=1; $i<=$total_pages; $i++) {
echo "<a href='displaydesign.php?page=".$i."'>".$i."</a> ";
};
// Going to last page
echo "<a href='displaydesign.php?page=$total_pages'>".'Last Page'."</a></center> ";
?>
</div>
</body>
I've been looking over your code, and I also setup an SQL-Fiddle (see below) to play around. I was initially thinking that maybe the MySQL query you use doesn't filter right on specific values or doesn't paginate right but filters right. Anyway there are also comments in the fiddle that explain some ideas and assumptions I made.
For what it's worth, pagination and complex SQL queries are non-trivial. They can also be expensive, and while I don't know your current code platform/setup/environment, I would suggest to push logic and things like pagination to the front end or JavaScript wherever possible. PHP works, too, but I'm mostly trying to point out that in my experience complexity for SQL queries often increases more quickly than code because it's a different type of frameset, namely relational versus procedural.
Your code also contains view and business logic together as well as SQL, so to be open with you I might not find the issue or there may be more than one. But I did notice some things looking at your code.
(1) The paginated results for your contacts list query were NOT exclusive by page number - meaning in some cases a row from a previous result would be included in the results.
(2) There is weird behavior if there are every any empty values for your WHERE conditions values -- specifically, $dname, $dcity, $dlocation. I didn't manage to reproduce your exact issue from JUST the SQL in the SQL fiddle, but I'm wondering if you'll see the same issues with some cleanup to the list query.
Fix Suggestions:
Modify SQL query to use rank and an inner-query to exclusive-paginate results and eliminate empty-match issues. In my suggested query below and in the fiddle, I've adjusted the WHERE condition from a AND (b or c) AND d to a AND ( (b or c) or d ) -- while this looks weird, it says "make sure we're matching on a non-empty location (assumed primary selector), or that we have a name or city to filter by". At least one variable must match true for the LIKE condition checks or it won't return a result row for it, whereas the other way required all three matches for a row (which is fine if those values will always be non-empty).
SET #name = "";
SET #city = "";
SET #location = "WA";
SET #per_page = 1;
-- Setting this arbitrarily - looking at different pages
-- mostly verifying yeah, the fetch/ pagination part works.
SET #page = 3;
SET #rownum = 0;
-- Basic Fetch Query used for getting list-for-page
SELECT
results.id, results.name,
results.location, results.city,
results.rank
FROM (
SELECT
c.id AS id
,c.name AS name
,c.location AS location
,c.city AS city
,#rownum := #rownum + 1 AS rank
FROM contacts AS c
-- ,(SELECT #rownum := 0) r
WHERE c.id > 0
AND ( (c.name LIKE #name OR c.city LIKE #city)
OR c.location LIKE #location )
ORDER BY c.id ASC
) AS results
WHERE rank BETWEEN ( (#page-1) * #per_page + 1) AND ( (#page) * #per_page )
Clarification
There are 2 new things going on in this bigger query. One, there is something called an "inner query" being used to pre-select a set of rows. Second, I'm assigning a rank value to use in my final results query to organize the table rows into "paged" results. This is a workaround to trying to use
WHERE ...
LIMIT ((#page-1)*#per_page), #per_page
or even
WHERE ...
LIMIT ((#page-1)*#per_page) OFFSET #per_page
But (see my CREDITS section), apparently one CANNOT use #vars in SQL. You could also work around this in your code by using PHP string templating or something, like you're already doing to inject variables into strings.
SQL Fiddle
Here's that SQL Fiddle to see what assumptions I made and what I was doing:
http://sqlfiddle.com/#!9/d516e/26
Credits
Apparently, one cannot use user variables like #name in the LIMIT-clause, which I did not know before. I found another Stack post to help me with that problem:
Using variable in a LIMIT clause in MySQL

Debug PHP code with if, while, arrays for showing data [duplicate]

This question already has answers here:
How can I get useful error messages in PHP?
(41 answers)
Closed 6 years ago.
I'm creating some code to display all the records from a MySQL database into a PHP view.
The query is perfect: using HeidiSQL it retrieves all the values needed:
At the moment, is giving the following error:
How can I debug this?
The code:
<?php $sql = "select * from specials group by Start_Date DESC";
$result = #mysqli_query($sql)
if($result){
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo '<div class='deal-item col-md-12'>
<div class='col-md-4'>
<img src='lascruces_styles/img/deals-img/oil-deal.jpg' alt='' class='deal-thumb'>
<p class='expire'>The deal expires '.$row['End_Date'].'</p>
</div>
<div class='col-md-6 info-container'>
<h2 class='deal-title'>'.$row['Special_Name'].'</h2>
<p class='offer-user'>Offered by
<a href=''>'.$row['Added_By'].'</a></p>
<p class='deal-desc'>'.$row['Description'].'</p>
<div class='share-row'>
<a href='' class='share'>Share this deal</a>
<div class='social'>
<i class='icon-facebook'></i>
<i class='icon-gplus'></i>
<i class='icon-linkedin'></i>
<i class='icon-mail-squared'></i>
</div>
</div>
</div>
<div class='col-md-2 view-deal-container'>
<p class='old-price'>'.$row['Normal_Price'].'</p>
<p class='current-price'>'.$row['Special_Price'].'</p>
<a href=''><div class='view-deal'>
<p>VIEW DEAL</p>
</div>
</a>
</div>
</div>';
mysqli_free_result ($result); // Free up the resources }
}
?>
You're missing a couple of things.The main things are that you're missing the connection parameter in your mysqli_query() call, and your single quotes are messing all your syntax. Try in this manner:
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "select * from specials group by Start_Date DESC";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo '<div class="deal-item col-md-12">
<div class="col-md-4">
<img src="lascruces_styles/img/deals-img/oil-deal.jpg" alt="" class="deal-thumb">
<p class="expire">The deal expires ' . $row['End_Date'] . '</p>
</div>
</div>';
}
} else {
echo "0 results";
}
mysqli_close($conn);
You're not quoting your string properly, which is causing syntax errors. You're also mixing mysql_* with mysqli_* here.
I'm assuming the MySQLi connection you're using is called $mysqli here, change it accordingly (or if you're using another API, you need to adapt to use that, APIs don't mix).
Properly quoted it should look something like this.
<?php
$sql = "select * from specials group by Start_Date DESC";
if ($result = mysqli_query($mysqli, $sql)) {
while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
echo "<div class='deal-item col-md-12'>
<div class='col-md-4'>
<img src='lascruces_styles/img/deals-img/oil-deal.jpg' alt='' class='deal-thumb'>
<p class='expire'>The deal expires ".$row['End_Date']."</p>
</div>
<div class='col-md-6 info-container'>
<h2 class='deal-title'>".$row['Special_Name']."</h2>
<p class='offer-user'>Offered by
<a href=''>".$row['Added_By']."</a></p>
<p class='deal-desc'>".$row['Description']."</p>
<div class='share-row'>
<a href='' class='share'>Share this deal</a>
<div class='social'>
<i class='icon-facebook'></i>
<i class='icon-gplus'></i>
<i class='icon-linkedin'></i>
<i class='icon-mail-squared'></i>
</div>
</div>
</div>
<div class='col-md-2 view-deal-container'>
<p class='old-price'>".$row['Normal_Price']."</p>
<p class='current-price'>".$row['Special_Price']."</p>
<a href=''><div class='view-deal'>
<p>VIEW DEAL</p>
</div>
</a>
</div>
</div>";
}
mysqli_free_result ($result); // Free up the resources
}
?>

Some HTML tags disappear when i use PHP

I'm trying to generate dynamic contents fill from mysql db
here is my php code:
<?php
include 'header.php';
error_reporting(1);
$user = "root";
$pass = "";
$dbName = "haimi";
mysql_connect('localhost', $user, $pass) or
die("Could not connect: " . mysql_error());
mysql_select_db($dbName);
$sql = "SELECT * FROM Projects ";
$result = mysql_query($sql);
?>
<?php
while ($line = mysql_fetch_array($result)) {
?>
<li class="filter" data-filter=".cat<?php echo $line['Category'];?>"><?php echo $line['Category'];?></li>
<?php
}
?>
The li displays correctly, but the following does not:
<div class="row projects m0">
<?php
while ($line = mysql_fetch_array($result)) { ?>
<div class="project mix catHouses">
<div class="tint"></div>
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?> data-
lightbox="project" data-title="Central Hospital (building)">
<img src="images/projects/".<?php echo
$line['ProjectImage1']; ?> alt="<?php echo $line['ProjectTitle'];?>"
class="projectImg"> </a>
<div class="projectDetails row m0">
<div class="fleft nameType">
<div class="row m0 projectName"><?php echo $line['ProjectTitle'];?></div>
<div class="row m0 projectType"><?php echo $line['ProjectType'];?></div>
</div>
<div class="fright projectIcons btn-group" role="group">
<a href="images/projects/<?php echo $line['ProjectImage1']; ?>" data-lightbox="project" data-title="Central Hospital (building)" class="btn btn-default">
<i class="fa fa-link"></i></a>
<i class="fa fa- search"></i>
</div>
</div>
</div>
<?php
}
?>
</div>
It data in the divs doesn't appear.
You're making a single call, but trying to loop through it twice. To do so, you need to reset the pointer back to the beginning:
//Add this after the first loop, but before the second
mysql_data_seek( $result, 0 );
The way you have it now, it's while($line = mysql_fetch_array($result)), but the second loop is never entered since it has already reached the end. Since the loop is ended, it never displays the contents.
Important Note
The mysql_* functions are deprecated, and is removed in PHP 5.5. You should use Mysqli or PDO. They have better protections against mysql injections (see Bobby Tables).
You have some HTML mistakes here:
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?>
First, there is no need to use . operator (as you are in HTML, not PHP),
Also you shoud put your <?php ?> tag inside the href quotations, here is the correct code:
<a
href="images/projects/<?php echo $line['ProjectImage1']; ?>"
data-lightbox="project"
data-title="Central Hospital (building)"
>
<img
src="images/projects/<?php echo $line['ProjectImage1']; ?>"
alt="<?php echo $line['ProjectTitle']; ?>"
class="projectImg"
>
</a>
You will get older fast writing code like that ;)
How about this:
while ($line = mysql_fetch_array($result)) {
$category = $line['Category'];
echo <<< LOB
<li class="filter" data-filter="$category">$category</li>
LOB;
}
Like what Mr #matthew said
I was making a single call, and I was trying to loop through it twice.
The problem solved with this code before the while loop:
$result = mysql_query($sql);

HOWTO: get data from MYSQL database and then use/show this data in a php page in a while/foreach?

I figured out how to get the data, how to use it in a while loop, but I am stuck on the last part: a loop in a loop.
I have the following code:
<div id="tab1" class="tab_content">
<ul class="columns">
<?php
$result = mysql_query("SELECT * FROM TabContent WHERE TabID = 1");
while($row = mysql_fetch_array($result))
{
echo '<li><img src="images/layouts/'. $row['LayoutName'] .'.png" alt="" />
<div class="info">
<h2>'. $row['LayoutName'] .'</h2>
<p>'. $row['LayoutTiles'] . 'Tiles, '. $row['LayoutLayers'] . 'Layers</p>
</div>
</li>';
}
?>
</ul>
<div class="clear"></div>
<div class="bottom">
<div class="clearfix">
<div class="lfloat"></div>
<div class="rfloat"><a class="hide" href="#" onclick="return false">Cancel</a></div>
</div>
</div>
</div>
<div id="tab2" class="tab_content">
etc etc same as above
The problem I have is with the div with id="tab1" and the next one with id="tab2" etc. I can get an array of tabs using:
<?php
$result = mysql_query("SELECT * FROM Tabs");
while($row = mysql_fetch_array($result))
{
echo 'tab'. $row['TabID'] .;
}
?>
Giving me: Tab1 Tab2 Tab3 etc. Now the hard part, how do I use these so I can make a loop using this to replace my html div ids?
Here's a couple of tutorials that might help you in your struggle:
http://www.freewebmasterhelp.com/tutorials/phpmysql
http://www.homeandlearn.co.uk/php/php.html

Categories