i'm trying to paginate my results, the limit is working, the number of pages is properly set, but the links of the pagination don't work, i've been looking for a while and nothing, ¿can you take a look and tell me what i'm doing wrong? thanks
<?php
include("config/conexion.php");
$limit = 3;
if(isset($_GET['pag'])){
$pag= $_GET['pag'];
}else{
$pag=1;
}
$offset = ($pag-1) * $limit;
$sql = "SELECT SQL_CALC_FOUND_ROWS id, nombre, local, telefono, celular, email FROM almacenes WHERE id_cat = '".$_GET["id"]."'";
$sqlTotal = "SELECT FOUND_ROWS() as total";
$currentid = $_GET["id"];
$rs = mysql_query($sql);
$rsTotal = mysql_query($sqlTotal);
$rowTotal = mysql_fetch_assoc($rsTotal);
// Total de registros sin limit
$total = $rowTotal["total"];
?>
<?php if($_GET["id"]){ $cat = mysql_query("SELECT * FROM almacenes WHERE id_cat = '".$_GET["id"]."' ORDER BY id ASC LIMIT $offset, $limit"); if(mysql_num_rows($cat)>0){
while($row = mysql_fetch_object($cat)){ ?>
<div class="almacenbox">
<div class="shadow"></div>
<div class="white">
<div class="image"><img src=almacenes/local_111.jpg></div>
<div class="title"><?php echo $row->nombre?></div>
<div class="text">Local: <?php echo $row->local?></div>
<div class="text">Teléfono: <?php echo $row->telefono?></div>
<div class="text">Celular: <?php echo $row->celular?></div>
<div class="text"><?php echo $row->email?></div>
</div>
</div>
<?php } ?>
<?php } }else{ echo "<p>No hay resultados para mostrar</p>"; }?>
<table border="1" bordercolor="#000">
<tfoot>
<tr>
<td colspan="2">
<?php
$totalPag = ceil($total/$limit);
$links = array();
for( $i=1; $i<=$totalPag ; $i++)
{
$links[] = "<a href=almacenes.php?id=$currentid?pag=$i\>$i</a>";
}
echo implode(" - ", $links);
?>
</td>
</tr>
</tfoot> </table>
$links[] = "<a href=almacenes.php?id=$currentid?pag=$i\>$i</a>";
Should be
$links[] = '$i';
Query strings start with a ? but any name-value pairs after that first one require an ampersand.
On a side note you should never place user data directly into your query. This leaves you open to an SQL injection attack. Consider using mysql_real_escape_string or switching to the mysqli library.
Related
i have 3 table T1, T2, T3,
so, in T1 i want to select 1st row and select from T2 the rows related to the row selected in T1, and select from T3 rows related to T2 how have relation with row selected in T1 and print all this in one continer withe php while loop function
see the images for more descriptions
Base on what I understand with your question. This could be the answer
<?php
$db = new PDO("mysql:host=localhost", "username", "password");
$q1 = $db->prepare("SELECT * FROM T1");
$q1->execute();
$f1 = $q1->fetchAll(PDO::FETCH_ASSOC);
foreach ($f1 as $f1_items) {
echo "<tr><td>".$f1['id_cat']."</td></tr>";
$q2 = $db->prepare("SELECT * FROM T2 WHERE id_cat = ".$f1['id_cat'].";");
$q2->execute();
$f2 = $q2->fetchAll(PDO::FETCH_ASSOC);
foreach ($f2 as $f2_items) {
echo "<tr><td>\t".$f2['id_tr']."</td></tr>";
$q3 = $db->prepare("SELECT * FROM T3 WHERE id_tr = ".$f2['id_cus'].";");
$q3->execute();
$f3 = $q3->fetchAll(PDO::FETCH_ASSOC);
foreach ($f3 as $f3_items) {
echo "<tr><td>\t\t".$f2['id_cus']."</td></tr>";
}
}
}
?>
Solved and this is the code i am happy :)
$ReqFindRows = "SELECT * FROM commandes WHERE commande_status = 0 AND date(commande_le) = CURDATE()";
$STMTFindRows = $connect->stmt_init();
if(!$STMTFindRows->prepare($ReqFindRows)){
echo "No Results";
}
else {
$STMTFindRows->execute();
$ResultsFindRows = $STMTFindRows->get_result();
$x = 0;
while($ArrayCat = $ResultsFindRows->fetch_assoc()){
switch($ArrayCat['commande_importance']){
case 0 :
$importance = 'Normal';
$bgclas = "bg-blue";
break;
case 1 :
$importance = 'Urgent';
$bgclas = "bg-yellow";
break;
case 2 :
$importance = 'Immediat';
$bgclas = "bg-red";
break;
};
$prods = array();
$GetProductsInCommandeQuery = "SELECT * FROM commandes_produits WHERE commande_id = ?";
$GetProductsInCommandeSTMT = $connect->stmt_init();
if(!$GetProductsInCommandeSTMT->prepare($GetProductsInCommandeQuery)){
echo $connect->error;
}
else {
$id_toserch = $ArrayCat["commande_id"];
$GetProductsInCommandeSTMT->bind_param("s", $id_toserch );
$GetProductsInCommandeSTMT->execute();
$GetProductsInCommandeResults = $GetProductsInCommandeSTMT->get_result();
$Prodss = "";
while($GetProductsInCommandeArray = $GetProductsInCommandeResults->fetch_array()){
$prods = $GetProductsInCommandeArray["recette_id"];
$GetSupQuery = "SELECT * FROM commandes_supp WHERE tr_number = ? AND commande_id = ?";
$GetSupSTMT = $connect->stmt_init();
if(!$GetSupSTMT->prepare($GetSupQuery)){
echo $connect->error;
}
else {
$trNumber = $GetProductsInCommandeArray["tr_number"];
$GetSupSTMT->bind_param("ss", $trNumber, $id_toserch );
$GetSupSTMT->execute();
$GetSupResults = $GetSupSTMT->get_result();
$Supp = "";
while($GetSupArray = $GetSupResults->fetch_array()){
$Suppss = $GetSupArray["supp_id"];
$Supp .= '<p style="color:#F00">'.$Suppss.' </p>';
}
}
$Prodss .=
'<tr>
<td>
'.$prods.'
'.$Supp.'
</td>
<td>
A table
</td>
<td>
<button type="button" class="btn btn-sm">Servis</button>
</td>
</tr> ';
}
}
?>
<div class="col-xs-6 col-md-4" style="margin-bottom:10px;">
<div class="tiket">
<div class="tikeheader <?php echo $bgclas ?>">
<span class="commandenumber">
<i class="fa fa-star iconheader">
</i>
<?php echo $ArrayCat["commande_id"] ?> A Table
</span>
<span class="importance">
<?php echo $importance?>
</span>
</div>
<div class="tiketbody">
<table class="tiketbodytable">
<tbody>
<?php echo $Prodss ?>
</tbody>
</table>
</div>
</div>
</div>
<?php
}
}
?>
</div>
</div>
</div>
</div>
<?php
}
?>
enter image description here
How can I make a loop with php where I separate the comments as rounds
All comments for the round 1
<div id="$row['round']">
$row['comments']
</div>
All comments for the round 2
<div id="$row['round']">
$row['comments']
</div>
but I can have many rounds, so I don't know the exact query to use
<div id="chat" class="chat">
<ul class="nav nav-tabs">
<?php
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<li><a data-toggle="tab" href="#menu'.$row['round'].'">'.$row['round'].'</a></li>';
}
?>
</ul>
<div class="tab-content">
<?php
$i=-1;
$stmt = $DB->query("SELECT * FROM messages WHERE $id = video_id GROUP BY round ORDER BY round DESC");
while ($row = $stmt->fetch()) {
echo '<div class="tab-pane '.( $i = $i ? 'active' : '' ).'" id="menu'.$row['round'].'">';
$test = $row['round'];
$stmt2 = $DB->query("SELECT * FROM messages INNER JOIN system_users ON messages.user_id = u_userid WHERE $id = video_id AND $test = round ORDER BY date ASC");
while ($row2 = $stmt2->fetch()) { ?>
<br />
<p><b>round:</b> <?php echo $row2["round"]; ?></p>
<p><b>Message:</b> <?php echo $row2["message"]; ?></p>
<p><b>User:</b> <?php echo $row2["u_username"]; ?></p>
<p><b>time:</b> <?php echo date_format (new DateTime($row2["date"]), 'd|m|Y H:i:s'); ?></p>
<hr />
<?php
}$i =0;
echo '</div>';
}
?>
</div>
I am completely sure this is not the best solution, but it is working,
thanks anyway..
I tried to add a pagination script to my existing php page of sql queries.
But after adding the script the page is kept on loading without showing any content or error.
My code goes as:
<?php include('db.php'); ?>
<?php // define how many results you want per page
$results_per_page = 10;
// find out the number of results stored in database
$sql10='SELECT * FROM smf_messages';
$result10 = mysqli_query($conn, $sql10);
$number_of_results = mysqli_num_rows($result10);
// determine number of total pages available
$number_of_pages = ceil($number_of_results/$results_per_page);
// determine which page number visitor is currently on
if (!isset($_GET['page'])) {
$page = 1;
} else {
$page = $_GET['page'];
}
// determine the sql LIMIT starting number for the results on the displaying page
$this_page_first_result = ($page-1)*$results_per_page;
?>
Now the sql query codes to get the data from the respective tables...
<?php
$sql2 = "SELECT * FROM smf_log_digest WHERE note_type = 'topic' ORDER BY id_msg DESC LIMIT 420";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
while($row2 = $result2->fetch_assoc()) {
$number = $row2["id_msg"];
?>
This query relates to the content from which table to be retrieved..
<?php
// retrieve selected results from database and display them on page
$sql20='SELECT * FROM smf_messages WHERE id_msg = $number AND id_board = 4 LIMIT ' . $this_page_first_result . ',' . $results_per_page;
$result20 = mysqli_query($conn, $sql20);
while($row20 = mysqli_fetch_array($result20)) {
$member = $row20["id_member"];
$replies = $row20["id_topic"];
?>
<?php
$sqlstep1 = "SELECT COUNT(*) AS total FROM smf_log_digest WHERE note_type = 'reply' AND id_topic = $replies";
$rowNumstep1 = mysqli_query($conn, $sqlstep1);
$countstep1 = mysqli_fetch_assoc($rowNumstep1);
?>
// Body
<article class="well btn-group-sm clearfix">
<div class="topic-desc row-fluid clearfix">
<div class="col-sm-2 text-center publisher-wrap">
<img src="assets/images/profile.png" alt="" class="avatar img-circle img-responsive">
<h5><?php echo $row3["poster_name"]; ?></h5>
<small class="online">Member</small>
</div>
<div class="col-sm-10">
<header class="topic-footer clearfix">
<!-- end tags -->
</header>
<!-- end topic -->
<h4> <?php echo $row20["body"]; ?></h4>
<div class="blog-meta clearfix">
<small><?php echo $countstep1["total"]; ?> Replies</small>
<small><?php echo date('m/d/Y', $row20["poster_time"]); ?></small>
</div>
View the topic →
</div>
</div>
</article>
//end of body
<?php
}
// display the links to the pages
for ($page=1;$page<=$number_of_pages;$page++) {
echo '' . $page . ' ';
}
?>
<?php }
} else {
echo "";
}
?>
Please note that the data base connections are all checked and are right..
Any help is appreciated..
add this on top then check for error.
error_reporting(E_ALL);
ini_set('desplay_errors','1');
I am trying to make a youtube like main page. With the code below I want to make videos that are recommended for my users.
The following code shows only a user's video.
<?php $query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
?>
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php } ?>
I want to show this section only one time.
<div class="onerilent"><img src="<?php echo $user_avatar;?>"><?php echo $user_name ;?> Recommended for you</div>
Anyone can help me in this regard ?
The easiest way would be with a counter, like this:
<?php
$query = "SELECT
user.uid,
user.user_name,
user.user_avatar,
user_posts.uid_dk,
user_posts.post_id,
user_posts.post_name,
user_posts.post_info,
user_posts.post_time,
user_posts.post_ext,
user_posts.post_num,
user_posts.post_views
FROM user
JOIN user_posts
ON user_posts.uid_dk = user.uid
WHERE user_name='$user_name' LIMIT 5";
$run_query = mysql_query($query);
$counter = 1;
while($data=mysql_fetch_assoc($run_query)){
$post_name=$data['post_name'];
$post_time = $data['post_time'];
$post_views = $data['post_views'];
$post_numid = $data['post_num'];
$post_id = $data['post_id'];
$user_name = $data['user_name'];
$user_avatar = $data['user_avatar'];
if($counter == 1){
$counter++;
echo '<div class="onerilent"><img src="'.$user_avatar.'">'.$user_name.' Recommended for you</div>';
}
?>
<div class="onmnwrp">
<div class="onmn">
<div class="onmn_img"><img src="<?php echo $base_url.'user_uploads/'.$post_num;?>.png"></div>
<div class="onmg_tit"><?php echo $post_name;?></div>
<div class="onm_snm">gönderen: <?php echo $user_name;?></div>
<div class="onm_tim"><?php echo $post_views;?> views</div>
</div>
</div>
<?php
}
?>
note the $counter variable is set to 1 before the loop, and inside the loop there is a condition to check if it is set to the value 1, and if it is, then it echo's your html and increments the $counter, so that it is no longer set to 1
The query am running against my database to get the 3 records order it by Random. The problem is that sometimes it shows all 3 records sometimes it only shows 2, 1 and other times its just blank. In the database I have around 28 records.
What I have tried
I have tried without LIMIT - Problem Same
I have echoed out $suggested_profile_id found all 3 records coming out.
This is the query that gets the records LIMIT it by 3
<?php
$sql = "SELECT * FROM members WHERE member_status='activated' ORDER BY RAND() DESC LIMIT 3";
$query = $db->SELECT($sql);
if($db->NUM_ROWS() > 0){
$rows = $db->FETCH_OBJECT();
?>
This is the code that runs and gets all 3 records in a loop.
<!-- Suggested Friends -->
<div class="col-md-0 media-body">
<?php
foreach($rows as $row){
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT();
foreach($rows as $row){
$suggested_profile_id = $row->profile_id;
$suggested_profile_photo = $row->profile_photo;
$suggested_profile_username = $row->profile_username;
$suggested_profile_name = $row->profile_name;
if(
$suggested_profile_id != GET_SESSION_ID_VALUE(ENCRYPTION_KEY)&&
!is_in_ARRAY($make_string_to_ARRAY, $suggested_profile_id)
){
?>
<div class="row margin0">
<div class="col-md-4 pad0">
<a href="/<?php echo $suggested_profile_username; ?>" title="<?php echo $suggested_friends_profile_name; ?>" >
<?php
global $suggested_friends_profile_id;
$member_dir = dirname(dirname(dirname(__FILE__))) . "/members/" . $suggested_profile_id ."/smalll_" . $suggested_profile_photo;
if(file_exists($member_dir)){
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/members/<?php echo $suggested_profile_id; ?>/smalll_<?php echo $suggested_profile_photo; ?>" width="50" height="50">
<?php
} else {
?>
<img alt="<?php echo $suggested_profile_name; ?>" title="<?php echo $suggested_profile_name; ?>" src="/assets/images/default.jpg" width="50" height="50">
<?php
}
?>
</a>
</div>
<div class="col-md-8 pad0">
<?php echo $suggested_profile_name; ?>
<span class="f12 gray">271 Mutual Friends</span>
Add as friend
</div>
</div>
<?php
}
}
}
?>
</div>
<!-- ** Suggested Friends -->
What am I missing? Is there any alternative way I can achieve this...thanks!
It looks to me like you're overwriting your $rows variable within the inner select.
foreach($rows as $row){ // <-- first $rows / $row
$member_id = $row->member_id;
$sql = "SELECT * FROM profile WHERE profile_id='$member_id' LIMIT 1";
$query = $db->SELECT($sql);
$rows = $db->FETCH_OBJECT(); <-- $rows overwritten
foreach($rows as $row){
Break your display from your application logic and you won't have such a hard time debugging this kind of thing. Besides, you have a lot of duplicated code and that makes things hard to manage as well as being hard to debug.
Further, you wouldn't have this problem if you ran one query: SELECT * FROM members JOIN profile ON members.member_id = profile.profile_id and not only does your code get simpler and your double-foreach loop problem disappear, but your database access will also be a lot more efficient.