I am trying to build a CMS system where an admin inserts data in a template. There can be as many pages created with that template as the admin wants. The pages should then be accessed in a menu with links and be displayed with the respective data according the link you just clicked.
Everything is working as expected except the system is not picking up the data from the id that the link had. Instead the system is picking up the data from the last id on the database.
Example: If I click on the link Page#2, I will have the URL http://www.website/page.php?pid=2 but the content that is loaded is the content from the last id on the database and not the 2.
Code to make and display the links:
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = preg_replace('/[^0-9]/', "", $_GET['pid']); // filter everything but numbers for security
}
$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die('Error: ' . mysqli_error($myConnection));
$listadeavatars = '';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$avatar = html_entity_decode($row["linklabel"]);
}
$sqlCommand = "SELECT id, linklabel FROM pages WHERE showing='1' ORDER BY id ASC";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
$menuDisplay = '';
while ($row = mysqli_fetch_array($query)) {
$pid = $row["id"];
$linklabel = $row["linklabel"];
$avatar = html_entity_decode($row["linklabel"]);
$menuDisplay .= '<li>' . $avatar . '</li>';
}
mysqli_free_result($query);
Code to display the data on page.php according to the ID
if (!$_GET['pid']) {
$pageid = '1';
} else {
$pageid = ereg_replace("[^0-9]", "", $_GET['pid']); // filter everything but numbers for security
}
$sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pid' LIMIT 1";
$query = mysqli_query($myConnection, $sqlCommand) or die (mysqli_error());
while ($row = mysqli_fetch_array($query)) {
$pagetitle = $row["pagetitle"];
$linklabel = $row["linklabel"];
$evenemang = $row["evenemang"];
$presentation = $row["presentation"];
$producent = $row["producent"];
$pagebody = $row["pagebody"];
$mapa = $row["mapa"];
$avatar = html_entity_decode($row["linklabel"]);
}
mysqli_free_result($query);
Please do let me know if I need to explain better! Any suggestions to solve this issue? Cheers!
On page.php, change WHERE id='$pid'
$sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pid' LIMIT 1";
to WHERE id='$pageid'
$sqlCommand = "SELECT pagetitle, linklabel, evenemang, presentation, producent, pagebody, mapa FROM pages WHERE id='$pageid' LIMIT 1";
Related
i am having category sidebar below code
Working Code:
//Showing categories function
function getCats(){
global $db;
$q_cats = "select * from cats ORDER BY cat_name ASC";
$run_cats = mysqli_query($db, $q_cats);
while($row_cat = mysqli_fetch_array($run_cats)){
$cat_id = $row_cat['cat_id'];
$cat_name = $row_cat['cat_name'];
$q_count = "select * from messages where cat_id='$cat_id'";
$run_count = mysqli_query($db, $q_count);
$cat_sms_count = mysqli_num_rows($run_count);
if($cat_sms_count < 1){
$cat_sms_count = 0;
}
echo "<a href='category.php?category=$cat_id' class='list-group-item'>$cat_name<span class='badge'>$cat_sms_count</span></a>";
}//while row END here
$q_tmsgs = "select * from messages";
$run_tcount = mysqli_query($db, $q_tmsgs);
$tsms_count = mysqli_num_rows($run_tcount);
echo "<a href='allindb.php' class='list-group-item t-list'>Total Messages in DB<span class='badge'>$tsms_count</span></a>";
}//Function getCats END here
this working fine but what i want, i want to add a new badge image next to the category name. i tried a lot but failed to do, what i think is the most relevant code.
Please check this code:
//Showing categories function
function getCats(){
global $db;
$q_cats = "select * from cats ORDER BY cat_name ASC";
$run_cats = mysqli_query($db, $q_cats);
while($row_cat = mysqli_fetch_array($run_cats)){
$cat_id = $row_cat['cat_id'];
$cat_name = $row_cat['cat_name'];
$q_count = "select * from messages where cat_id='$cat_id'";
$run_count = mysqli_query($db, $q_count);
$cat_sms_count = mysqli_num_rows($run_count);
//problem area----------------------------------
$q_new = "select * from messages where cat_id='$cat_id' ORDER BY date DESC LIMIT 20";
$run_new = mysqli_query($db, $q_new);
while($new = mysqli_num_rows($run_new)){
$newbadge = $new['cat_id'];
}
if($cat_id==$newbadge){
$newbadge1 = "<img src='images/mynew.gif'>";
}
//--------------------------------------------
if($cat_sms_count < 1){
$cat_sms_count = 0;
}
echo "<a href='category.php?category=$cat_id' class='list-group-item'>$cat_name $newbadge1<span class='badge'>$cat_sms_count</span></a>";
}//while row END here
$q_tmsgs = "select * from messages";
$run_tcount = mysqli_query($db, $q_tmsgs);
$tsms_count = mysqli_num_rows($run_tcount);
echo "<a href='allindb.php' class='list-group-item t-list'>Total Messages in DB<span class='badge'>$tsms_count</span></a>";
}//Function getCats END here
If i understand you correctly you want to show "new badge" on those topics that doesnt have any messages in them. if so you got $q_count
Just replace the while{} inside with an if{}else{}like so:
if($q_count == 0) {
$newbadge1 = "<img src='images/mynew.gif'>";
} else {
$newbadge1 = '';
}
The problem is that you are doing another query that is returning empty array, and therefore you are getting badges on topics that has got messages. So either remove the while{} and replace with the if-else or you can just do a check like:
if(empty($new)) {
$newbadge1 = "<img src='images/mynew.gif'>";
} else {
$newbadge1 = '';
}
I know how to fetch user data from database with the code below. Now I want to navigate to another page (using onclick) and to display this user data by id. This would be like StackOverflow or Facebook, when you click on a photo or ID, and the site takes you to the user's profile page.
Here is my code so far:
<?php
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$select = mysql_select_db("profile") or die(mysql_error());
$result = mysql_query("SELECT * FROM users order by id DESC");
$id = $_SESSION['id'];
while($row = mysql_fetch_array($result)){
if($row['id'] !== $id){
echo "<table id='suggest'><tr><td id='frienddata'><a href='http://localhost/profile/userprofile.php'>".$row['first'].' '. $row['last']."<a/></td><br></tr></table>";
}
}
?>
$id = $_GET['id'];
if(!isset($id))
{
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$select = mysql_select_db("profile") or die(mysql_error());
$result = mysql_query("SELECT * FROM users WHERE id = '"$id"'");
if(!$result)
{
die('user_not_found');
}
mysqli_fetch_row( $result );
echo "<table id='sugest'><tr><td id='frienddata'><a href='http://localhost/profile/userprofile.php'>".$row['first'].' '. $row['last']."<a/></td><br></tr></table>";
suppose you are in a page before clicking on a user profile,the link should be some thing like this 'site.com/userprofile.php?id=5'.
now in userprofile.php:
$id = $_GET['id'];
if(!isset($id))
die('user not found');
$connect = mysql_connect("localhost","root","") or die(mysql_error());
$select = mysql_select_db("profile") or die(mysql_error());
$result = mysql_query("SELECT * FROM users where id='".$id."'");
if (!$result) {
die('user not found');
}
$row = mysql_fetch_row($result);
echo "<table id='sugest'><tr><td id='frienddata'><a href='http://localhost/profile/userprofile.php'>".$row['first'].' '. $row['last']."<a/></td><br></tr></table>";
I'm having a hard time getting this search results with pagination code to work. It does successfully grab the search keyword entered in the html form on another page and brings it into this search.php page. if I echo $search I see the keyword on the page. But I get no results even though I should for the query. Can anyone see what might be going on?
require "PDO_Pagination.php";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
echo $search;
$pagination->rowCount("SELECT * FROM stories WHERE stories.genre = $search");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories WHERE stories.genre = $search ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
else
{
$pagination->rowCount("SELECT * FROM stories");
$pagination->config(3, 5);
$sql = "SELECT * FROM stories ORDER BY SID ASC LIMIT $pagination->start_row, $pagination->max_rows";
$query = $connection->prepare($sql);
$query->execute();
$model = array();
while($rows = $query->fetch())
{
$model[] = $rows;
}
}
$query = "SELECT * FROM stories";
if(isset($_REQUEST["search_text"]) && $_REQUEST["search_text"] != "")
{
$search = htmlspecialchars($_REQUEST["search_text"]);
$pagination->param = "&search=$search";
$query .= " WHERE genre LIKE '%$search%'";
}
// No need for else statement.
$pagination->rowCount($query);
$pagination->config(3, 5);
$query .= " ORDER BY SID ASC LIMIT {$pagination->start_row}, {$pagination->max_rows}";
$stmt = $connection->prepare($query);
$stmt->execute();
$model = $stmt->fetchAll();
var_dump($model);
In your query do:
WHERE stories.genre LIKE '%string%');
instead of:
WHERE stories.genre = 'string');
Because the equals will want to literally equal the field.
I have been developing a social network, and a key function is to be able to post on other users' profiles. However, my current code will only show one post. Also, it seems to be the first post that is shown. I have tested this by creating new accounts, and writing a test post. The code does show this first post, but if I try it again, only the first post is visible. The code is as follows:
The code to send post to database:
$person = "profile.php?id={$id}";
$post = $_POST['post'];
if($post != "")
{
$data_added = date("Y-m-d");
$added_by = $session_username;
$user_posted_to = $id;
$post = preg_replace("#[^0-9a-z]#i", "", $post);
$sqlCommand = "INSERT INTO posts VALUES ('',
'$post',
'$data_added',
'$added_by',
'$user_posted_to')";
$commandQuery = mysql_query($sqlCommand) or die ("Couldn't send post");
}
else
{
echo "You have to fill in the post form...";
}
The code to retrieve it (and display it):
$getPosts = mysql_query("SELECT *
FROM posts
WHERE user_posted_to='$id'
ORDER BY id DESC LIMIT 15") or die("Couldn't find any posts");
while($row = mysql_fetch_array($getPosts))
{
$id = $row['id'];
$body = $row['body'];
$date_added = $row['date_added'];
$added_by = $row['added_by'];
$user_posted_to = $row['user_posted_to'];
$querya = mysql_query("SELECT *
FROM members
WHERE username='$added_by' LIMIT 1");
while($row = mysql_fetch_array($querya))
{
$user_added = $row['id'];
}
$user_added = "profile.php?id={$user_added}";
}
echo "
<div>
<h3><a href='$user_added'>$added_by</a> - $date_added </h3>
<p> $body</p>
</div><br />
";
If anyone needs some more of the code, like my database connection, just comment.
In your while cicle you fill in some variables but you do not use them.
You use echo only outside the cycle, so just once, and thus you print only the values of the last istance of the while cycle.
Try
$getPosts = mysql_query("SELECT * FROM posts WHERE user_posted_to='$id' ORDER BY id DESC LIMIT 15") or die("Couldn't find any posts");
while($row = mysql_fetch_array($getPosts)){
$id = $row['id'];
$body = $row['body'];
$date_added = $row['date_added'];
$added_by = $row['added_by'];
$user_posted_to = $row['user_posted_to'];
$querya = mysql_query("SELECT * FROM members WHERE username='$added_by' LIMIT 1");
while($row = mysql_fetch_array($querya)){
$user_added = $row['id'];
}
$user_added = "profile.php?id={$user_added}";
echo "
<div><h3><a href='$user_added'>$added_by</a> - $date_added </h3><p> $body</p></div><br />";
}
<?php
$tid = $_GET['tid'];
$id = $_SESSION['userid'];
$sql1 = "SELECT * FROM topics WHERE id='$tid' LIMIT 1";
$res1 = mysqli_query($connect, $sql1) or die(mysqli_error($connect));
while ($row = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
$title = $row['topic_title'];
$creator = $row['topic_creator'];
}
$sql = "SELECT * FROM users WHERE id='$creator' LIMIT 1";
$user_query = mysqli_query($connect, $sql) or die(mysqli_error($connect));
while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$name = $row["first"].$row["last"];
}
echo $name;
?>
I'm a little new to PHP, but I've done things exactly like this, but this time I'm getting an error. Everything here works except for $name. I checked my SQL tables and made sure users exist and that there's first and a last area. I don't see what else could be wrong.
Notice: Undefined variable: name in * on line **
Thank you.
Try this code on for size:
<?php
$tid = $_GET['tid'];
$id = $_SESSION['userid'];
$tid = mysqli_escape_string($connect, $tid);
$sql1 = "SELECT * FROM topics WHERE id='{$tid}' LIMIT 1";
$res1 = mysqli_query($connect, $sql1) or die(mysqli_error($connect));
// Check for rows first.
if($res1 and mysqli_num_rows($res1)){
// Use if as while is pointless on LIMIT 1
if($row = mysqli_fetch_array($res1, MYSQLI_ASSOC)) {
$title = $row['topic_title'];
$creator = $row['topic_creator'];
$creator = mysqli_escape_string($connect, $creator);
$sql = "SELECT * FROM users WHERE id='{$creator}' LIMIT 1";
$user_query = mysqli_query($connect, $sql) or die(mysqli_error($connect));
// Check for rows first.
if($user_query and mysqli_num_rows($user_query)){
// Use if as while is pointless on LIMIT 1
if ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {
$name = $row["first"].$row["last"]; // NO HIT!
}
echo $name;
}else{
echo 'no rows found (query 2).';
}
}
}else{
echo 'no rows found (query 1).';
}
?>
Variable $name is undefined because the $name = ...; line is not reached. So make sure you $sql query actually returns results. It has to in order to define $name.