I am trying to link through to the single row record information in my mysql database from my search using php/ajax_pagination and the code that I am trying to get to work is:
<div id="posts_content">
<?php
//Include pagination class file
include('Pagination.php');
//Include database configuration file
include('dbConfig.php');
$limit = 3;
//get number of rows
$queryNum = $db->query("SELECT COUNT(*) as postNum FROM posts");
$resultNum = $queryNum->fetch_assoc();
$rowCount = $resultNum['postNum'];
//initialize pagination class
$pagConfig = array(
'totalRows' => $rowCount,
'perPage' => $limit,
'link_func' => 'searchFilter'
);
$pagination = new Pagination($pagConfig);
//get rows
$query = $db->query("SELECT * FROM posts ORDER BY id DESC LIMIT $limit");
if($query->num_rows > 0){ ?>
<div class="posts_list">
<?php
while($row = $query->fetch_assoc()){
$postID = $row['id'];
?>
<div class="list_item"><h2><?php echo $row["title"]; ?></h2></div>
<?php } ?>
</div>
<?php echo $pagination->createLinks(); ?>
<?php } ?>
</div>
There is obviously something wrong with my href link:
<h2><?php echo $row["title"]; ?></h2>
OR my file.php page:
<?php
// GET ID FROM THE URL
$id = $_GET['id'];
?>
<!DOCTYPE HTML>
<html lang="en">
<head>
<title>Ajax Pagination with Search and Filter in PHP</title>
<link href='style.css' rel='stylesheet' type='text/css'>
<script src="jquery.min.js"></script>
<script type="text/javascript"></script>
</head>
<body>
<?php
$sql =mysql_query("select * from posts where id='".$id."'");
while($row = mysql_fetch_array($sql)){
?>
<tr>
<th>title:</th>
<th>created:</th>
<th>modified:</th>
<th>statusr:</th>
</tr>
<tr>
<td><?=$row['title']?></td>
<td><?=$row['created']?></td>
<td><?=$row['modified']?></td>
<td><?=$row['status']?></td>
</tr>
<?php
}
?>
But I cannot understand why as everything else seems to be working fine, maybe I have been looking at this to long and missed something blatently obvious... but any help would be greatly appreciated.
Use Below Code:
<div class="list_item"><h2><?php echo $row["title"]; ?></h2></div>
Related
I am trying to join two tables together. In posts table I have two column post_id and post_message.
In another table name post_attach have row_id, postid(same as post_id), and file_name( As like ms.jpg).
A post may have several attachment.But I don't know what will be query to show a post with several attachment. Here is my query...
$s = " SELECT posts.post_id,post_message, post_attach.file_name from posts join post_attach on post_id=row_id;
<?php
$conn = mysqli_connect('localhost', 'root', 'root', 'blog') or die();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h3>Allo post with their attahcment</h3>
<?php
$s = " SELECT posts.post_id,post_message, post_attach.file_name from posts join post_attach on post_id=row_id;
";
$q = mysqli_query($conn,$s);
while ($row = mysqli_fetch_array($q)) {?>
<p>Post No: <?php echo $row['post_id'] ?></p>
<p>Post text: <?php echo $row['post_message'] ?></p>
<p>Post attach: <?php echo $row['file_name'] ?></p>
<?php } ?>
</body>
</html>
I want something like:
post no: 1, post text: hello php, attachment: ms.jpg, ms.jpeetc.
The final word is every post will show with their relative attachment.
I wouldn't join the tables outright. I would iterate through the main posts table and show the post id and post message, then loop through the attachments. Something like:
<?php
$s = "SELECT posts.post_id,post_message FROM posts";
$q = mysqli_query($conn,$s);
while($row = mysqli_fetch_array($q))
{
?>
<p>Post No: <?php echo $row['post_id'] ?></p>
<p>Post text: <?php echo $row['post_message'] ?></p>
<p>
Post attach:
<?php
$att = "SELECT post_attach.file_name FROM post_attach WHERE post_id = $row[post_id]";
$files = mysqli_query($conn,$att);
while($row_files = mysqli_fetch_array($files))
{
echo $row_files['file_name'] . "<br>";
}
?>
</p>
<?php
}
?>
I wonder why the 2nd output $devNO gives me bool(false) which is empty. i have no idea about it. Am I wrong at the mysql_query() part?
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
var_dump($developer);
echo "<br>";
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
Here's my output:
Here I will show my full code for "games.php":
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/html">
<html>
<head lang="en">
<meta charset="utf-8" />
<title>Game List</title>
<link rel="stylesheet" href="css.css">
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap.min.css">
<!-- Optional theme -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/css/bootstrap-theme.min.css">
<style>
legend {font-family: "Palatino Linotype", "Book Antiqua", Palatino, serif}
</style>
</head>
<body>
<div id="big_wrapper">
<header id="top_header">
<h1>Dandy's Game Library</h1>
</header>
<nav id="top_menu">
<ul>
<li><strong></stron>Home</strong></li>
<li><strong>Game List</strong></li>
</ul>
</nav>
<div id="game_wrapper">
<section id="filter">
<form action="games.php" method="post" name="search_form">
<fieldset>
<legend><h3><strong>Search</strong></h3></legend>
<strong>Developer</strong><br>
<select name="dev">
<option value="">--Select--</option>
<?php
include("dbConnection.php");
mysql_connect("localhost","root","");
mysql_select_db("games");
$sql = mysql_query("SELECT Publisher FROM publisher");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Publisher']."'>" .$row[Publisher]. "</option>";
}
?>
</select>
<br/><br/><strong>Game Platform</strong><br>
<select name="plat">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Platform FROM platform");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Platform']."'>" .$row[Platform]. "</option>";
}
?>
</select>
<br/><br/><strong>Genre</strong><br>
<select name="gen">
<option value="">--Select--</option>
<?php
$sql = mysql_query("SELECT Genre FROM genre");
while($row = mysql_fetch_array($sql)){
echo "<option value='".$row['Genre']."'>" .$row[Genre]. "</option>";
}
$developer = $_POST['dev'];
$platform = $_POST['plat'];
$genre = $_POST['gen'];
?>
</select>
<br><br><input type="submit" name="search" value="Search"></input>
</fieldset>
</form>
</section>
<aside id="items">
<fieldset>
<legend><h3><strong>Game List</strong></h3></legend>
<?php
var_dump($developer);
$devNO = mysql_query("SELECT No FROM developer WHERE Developer = $developer");
$platNO = mysql_query("SELECT No FROM platform WHERE Platform = $platform");
$genNO = mysql_query("SELECT No FROM genre WHERE Genre = $genre");
var_dump($devNO);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>
<div class="row">
<div class="cell"><?php echo $row[$game_title]?></div>
<div class="cell"><?php echo "Year : ".$row[$game_year]?></div>
<div class="cell"><?php echo "Language : ".$row[$game_lan]?></div>
<div class="cell"><?php echo "Price : RM".$row[$game_price]?></div>
</div>
<?php
}
?>
</div>
</fieldset>
</aside>
</div>
</div>
<div id="btm_wrapper">
<footer id="foot">
<strong></strong>
</footer>
</div>
</body>
</html>
The full output will be like this after i search from dropdown:
The method mysql_query() , if successful, returns a resultset, which has to be made an array even if only a single value is being returned from the query.
Ideally there should be a for-each iteration on this array to get result, however if you're sure you'll get at least one value, the first row with desired index('No' in your case).
$dataset= mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$row = mysql_fetch_assoc($dataset);
$devNO = $row['No'];
Also note the quote I've used for varchar. It was correctly pointed in the other answer. Try Now.
If $developer is a string use quotes. You need to first fetch results from result set and than use it in code:
<?php
$devNOResult = mysql_query("SELECT No FROM developer WHERE Developer = '$developer'");
$platNOResult = mysql_query("SELECT No FROM platform WHERE Platform = '$platform'");
$genNOResult = mysql_query("SELECT No FROM genre WHERE Genre = '$genre'");
$devNO = mysql_fetch_row($devNOResult);
$sql = sprintf("SELECT Title, Release_Year, Language, Price FROM games WHERE Developer_NO = $devNO");
$result = mysql_query($sql);
$game_title = 'Title';
$game_year = 'Release_Year';
$game_lan = 'Language';
$game_price = 'Price';
?>
<div id="gamelist">
<?php
if(!$result) {
die(mysql_error());
}
while($row = mysql_fetch_array($result)) {
?>
I am trying to add stuff into an array after every click a user makes on a category but for some reason it keeps replacing everything in the array. I can't figure out where I am going wrong. I've tried 'googling' it and every example i find looks similar to what i have written. Please Help!
these functions are store in core.php
function getStoreBacktrace($cat) {
include("config.php");
$backtrace = array();
if ($cat != 0) {
array_push($backtrace, $cat);
}
if (count($backtrace != 0)) {
foreach($backtrace as $c){
echo getBackCatName($c);
}
}
print_r($backtrace); // Put this to see what output is
}
function getBackCatName($c) {
include("config.php");
$query = 'SELECT * FROM `home_store_cats` WHERE `id`="'.$c.'"';
$r_query = mysql_query($query);
$result = mysql_fetch_array($r_query);
echo ' > '.$result['name'].'';
}
this function prints out a list of links the user can click on
function getStoreCat($cat) {
include("config.php");
$query = 'SELECT * FROM `home_store_cats` WHERE `main`="'.$cat.'" ORDER BY `name` ASC';
$r_query = mysql_query($query);
echo '<ul>';
while ($result = mysql_fetch_array($r_query)) {
echo '<li>';
echo ''.$result['name'].'';
echo '</li>';
}
echo '</ul>';
}
and it gets called in store.php
<?php
include("config.php");
include("core.php");
$backtrace = array();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title><?php echo getSiteTitle().' :: '.getSiteSlogan(); ?></title>
</head>
<body>
<table width="100%">
<tr>
<td colspan="2">
<!-- Backtrace -->
Home
<?php echo getStoreBacktrace($cat, $backtrace); ?>
</td>
</tr>
<tr>
<td>
<!-- Categories -->
<table>
<tr>
<td><?php echo getStoreCat($cat); ?></td>
</tr>
</table>
</td>
<td>
<!-- Products -->
<table>
<tr>
<?php echo getStoreProducts($cat); ?>
</tr>
</table>
</td>
</tr>
</table>
</body>
</html>
now everytime a user clicks on a link that is made from the function getStoreCat($cat) it refreshes the page with new links to click on and new products to show depending on what $cat they chose. i want to push the $cat to the $backtrace array.
Right here:
$backtrace = array();
You are effectively resetting the array for each call to getStoreBacktrace().
EDIT:
Thanks for fixing your question. Now it's clear that the issue is you need to make $backtrace persistent through multiple page views. Do this using sessions:
Page template
<?php
session_start(); // enable sessions
include("config.php");
include("core.php");
?>
<!DOCTYPE html>
etc...
Function definition
<?php
function getStoreBacktrace($cat) {
include_once("config.php"); // use include_once() to prevent possible errors
if (!isset($_SESSION['backtrace']))
$_SESSION['backtrace']= array();
if ($cat != 0) {
array_push($_SESSION['backtrace'], $cat);
}
...
Everytime you call getStoreBacktrace you instantiate a brand new array in $backtrace.
I have a site that will have pages for various countries and regions. On a main page I'm looking to populate an HTML table that is 4 columns by 12 rows (48 results) from an array. I current have a loop working that is placing each country into it's own paragraph but I'm lost on how to place them into the table and have them setup in the 4x12 way I described above.
Here is the query:
$country_sql = "SELECT * FROM country";
$country_query = mysql_query($country_sql) or die(mysql_error());
$rsCountry = mysql_fetch_assoc($country_query);
Then within the page I have this working right now that inserts each country into a new paragraph:
<?php do {?>
<p><?php echo $rsCountry['countryname'];?></p>
<?php } while ($rsCountry = mysql_fetch_assoc($country_query)) ?>
First I need to have this place data into seperate td's instead of new paragraphs. Once I have this data in a table I then need the country names link to each countries respective page which is setup as mydomain.com/country/countryabbreviation (countryabbreviation is within the same table as countryname). Any help on this would be greatly appreciated as I'm very new to php. Thanks!
----Full Code---
<?php
///CONNECTION INFORMATION REMOVED FOR PRIVACY//////////
$country_sql = "SELECT * FROM country";
$country_query = mysql_query($country_sql) or die(mysql_error());
$rsCountry = mysql_fetch_assoc($country_query);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" content="text/html">
<title>TEST PAGE </title>
</head>
<body>
<h1>List of Countries</h1>
<?php do {?>
<p><?php echo $rsCountry['countryname'];?></a></p>
<?php } while ($rsCountry = mysql_fetch_assoc($country_query)) ?>
</body>
</html>
Outputs this:
http://postimg.org/image/f2muo627d/
This should do the trick:
<?php
// Rewrote the query to only get 48 items.
$country_sql = "SELECT * FROM country LIMIT 48";
$country_query = mysql_query($country_sql) or die(mysql_error());
// Initialize an empty array.
$rsCountry = array();
// Put all our countries in there.
while ($row = mysql_fetch_assoc($country_query)) {
$item = array();
$item['href'] = '/country/' . $row['countryabbreviation'];
$item['title'] = $row['countryname'];
$rsCountry[] = $item;
}
// Chop up our massive array in rows of 4, should give us 12 rows.
$rsCountry = array_chunk($rsCountry, 4);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" content="text/html">
<title>TEST PAGE</title>
</head>
<body>
<table>
<thead>
<tr>
<td colspan="4">
<h1>List of Countries</h1>
</td>
</tr>
</thead>
<tbody><?php
foreach ($rsCountry as $row => $countries): ?>
<tr><?php
foreach ($countries as $country): ?>
<td><?= echo $country['title'] ?></td><?php
endforeach; ?>
</tr><?php
endforeach; ?>
</tbody>
</table>
</body>
</html>
is this what you want? I didn't test if it compiles
$country_sql = "SELECT * FROM country";
$country_query = mysql_query($country_sql) or die(mysql_error());
$rsCountry = mysql_fetch_array($country_query)
echo "<table><th>";
$firstrow ="";
foreach( $rsCountry as $name => $cell ){
echo "<td>$name</td>";
$firstrow = $firstrow + "<td>$cell</td>";
}
echo "</th>";
echo "<tr>$firsrow</tr>";
while( $rsCountry = mysql_fetch_array($country_query) ){
echo "<tr>";
foreach( $rsCountry as $cell ){
echo "<td>$cell</td>";
}
echo "</tr>";
}
echo "</table>";
In my project I have done a pagination using PHP, I have done it as follows:
Each buttons(NEXT PAGE and PREVIOUS PAGE) are hyperlinks which links to the same page with passing a GET variable (PAGENUMBER). For each page change, there are scripts for
connect to DB .
query for the data for the specified page.
close the
DB connection. displays the data.
The problem is that it is working slowly. Is there any alternative method for each time connect to db and query for the data and close db connection.
Also During the pagination I want to reload only the datas from DB and all other contents in page remains the same and dont want to reload. How to achieve this?
The script is given below
<?php
require_once("./include/membersite_config.php"); //include files for login system
if($fgmembersite->CheckLogin())
{
$login=true;
}
else
{
$login=false;
}
require_once("db.php");
$con=connect(); //connect to db and select the db
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML>
<HEAD>
<TITLE> Gallery </TITLE>
<META name="generator" content="Adobe Photoshop(R) CS Web Photo Gallery">
<META http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="images/galleryStyle.css" rel="stylesheet" type="text/css">
<link rel="STYLESHEET" type="text/css" href="style/new.css" />
</HEAD>
<body marginheight=0 marginwidth=0 leftmargin=0 topmargin=0>
<table><tr>
<?php
//////////////////DISPLAYING IMAGE THUNBNAILS//////////////////////
$query="SELECT COUNT(*) FROM gallery ";
$res = query($query);
$raw=mysql_fetch_array($res);
$imagecount=$raw[0];
if($imagecount==0) { die ("No images found"); }
$pagecount=ceil($imagecount/15);
$page = isset($_GET['page']) ? mysql_real_escape_string($_GET['page']) : 1;
if (isset($_GET['page']))
{
if (!(ctype_digit($_GET['page']) and $_GET['page']>0 and $_GET['page']<=$pagecount))
{
die ("You are not autorised to viw this page");
}
}
$start= ($page*15)-15;
$query = "SELECT * FROM gallery ORDER BY imageid DESC LIMIT $start , 15";
$res = query($query);
$count= mysql_num_rows($res);
$index=0;
for($i=1;$i<=3;$i++)
{
?>
<TR>
<?php
for($j=1;$j<=5;$j++)
{
if(!$raw=mysql_fetch_array($res)) {break;}
$index++;
$imageid=$raw['imageid'];
$thumbpath= $raw['thumbpath'];
$largepath=$raw['largepath'];
$caption=$raw['caption'];
?>
<A name=1 href="image.php?imageid=<?php echo $imageid ?>&imageindex=<?php echo ((($page-1)*15)+$index-1) ; ?> "><IMG border=0 src="<?php echo $thumbpath; ?>" height="75" width="75" alt="<?php echo $caption ?>" title="<?php echo $caption ?>" ></A><BR>
<?php
echo "<a href='delete.php?delete=yes&imageid=".$imageid.'&page='.$page."'>Delete </a>"
echo "<a href='replace.php?update=yes&imageid=".$imageid.'&page='.$page."'>Replace </a>"
?>
</table>
</td>
<?php } ?>
</TR>
<?php
}
if ($page!=1)
{
?>
<td width=17><IMG SRC="images/previous.gif" BORDER=0></td>
<?php } ?>
<td align=middle class="pagenums"><?php echo "page ".$page." of ".$pagecount ?></td>
<?php
if ($page!=$pagecount)
{
?>
<td align=right width=17><img border=0 src="images/next.gif"></td>
<?php } ?>
</table>
</body>
</html>
There are lot much options available
go with javascript pagination
ajax pagination
try to reduce pagination code(optimized code)
You do not need to connect/close db for each page change
use the ajax script for connecting and getting database values with reloading the current page you can do it -- -- -- --- -- -- try ajax