I'm creating a web site directory for my mobile site (FOUND HERE)
I have figured out how to display listings from my mysql table to my home page from my tables promo_cat list.
The thing im having trouble with is this:
once clicking on one of the catagories it leads me to my list.php page.
How do I get this page to display results related to the category clicked and not others?
For example:when clicking on "FREE" brings up this page: http://www.xclo.mobi/xclo2/list.php?id=FREE. Which displays all results. it should only display results that have a promo_cat field as "FREE" and should not display any other results as it does currently.
My list.php code:
<?php
include_once('include/connection.php');
include_once('include/article.php');
$article = new article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>xclo mobi</title>
<link rel="stylesheet" href="other.css" />
</head>
<body>
<?php include_once('header.html'); ?>
<div class="container">
Category = ???
<ol>
<?php foreach ($articles as $article) { ?>
<div class="border">
<a href="single.php?id=<?php echo $article['promo_title']; ?>" style="text-decoration: none">
<img src="<?php echo $article['promo_image']; ?>" border="0" class="img" align="left"><br />
<img alt="" title="" src="GO.png" height="50" width="50" align="right" />
<font class="title"><em><center><?php echo $article['promo_title']; ?></center></em></font>
<br /><br />
<font class="content"><em><center><?php echo $article['promo_content']; ?></center></em></font>
</div><br/><br />
</a>
<?php } ?>
</ol>
</div>
</body>
</html>
/include/article.php
<?php
class article {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($promo_title) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi WHERE promo_title = ?");
$query->bindValue(1, $promo_title);
$query->execute();
return $query->fetch();
}
}
?>
You need to make changes to the code for list.php based on the input it gets through GET parameter. something like:
if ($_GET['id'] == 'FREE'){
// do something like display FREE items
}
elseif($_GET['id'] == 'GIFT') {
// display GIFT items
}
else {
// perform some default action
}
This is to make it even more database driven (helpful when there are many categories):
$sql = "select * from categories where id = '".$_GET['id']."'";
if (mysql_results($sql)){
// do something
}
else {
// show error
}
Note that this is for demo only and in your code you should use PDO/MySQLI and prepared statements and not mysql_results function.
In light of more information provided by OP:
Change this
$articles = $article->fetch_all();
to
$articles = $article->fetch_data($_GET['id']);
in list.php and see if you get correct results.
Based on the code you provided, try this:
<?php foreach ($articles as $article) {
if ($article['promo_cat'] === 'FREE') { ?>
// Keep the rest of the code
//instead of <?php } ?> - put...
<?php } } ?>
Keep in mind, this is messy. But the foreach statement (I imagine) is being used to print out all posts. So, before printing out a post, you just check to see if the promo_title is FREE, GIFT, etc. If it's not, then it doesn't print that item.
You can make this more dynamic by passing in a $_GET variable (which you apparently are doing, but the code is never using this variable) with the current promo title and altering the conditional line to say
if ($article['promo_cat'] === $_GET['id'])
Hope that helps!
Related
I have almost created a fully working dynamic page using PHP OOP. I have successfully created a working menu - that is when a menu item is clicked, relevant text is displayed on the page. However, what I want is to have the home page text displayed by default and not solely clicking the Home link.
Here is the code I am using:
<?php
include_once('includes/connection.php');
include_once('includes/Article.php');
//instanstiating the article class and ssigning it to a variable
$article = new Article;
//assigning the contents of the method called fetch_all to the variable $articles
//this fetch_all method is inside the Articles class which is assigned to the above variable $article
$articles = $article->fetch_all();
?>
<!DOCTYPE html>
<html>
<head>
<title>Simple PDO CMS</title>
<link rel="stylesheet" type="text/css" href="assets/styles.css">
</head>
<body>
<div class="container">
<table class="topMenu">
<tr>
<td>
<h1 class="siteName">site name</h1>
</td>
<!--Displaying the articles using a foreach loop-->
<?php foreach ($articles as $article){ ?>
<td class="navItem">
<?php echo $article['menuheader']; ?>
</td>
<?php } ;?>
</tr>
</table>
</div>
<p> this is where the time line will go</p>
<?php
//instanstiating the article class and ssigning it to a variable
$newArticle = new Article;
//checking if the user clicked the menu link
if (isset($_GET['id'])) {
//the display the article content
$id=$_GET['id'];
$data=$newArticle->fetch_data($id);
?>
<p><?php echo $data['bodytext']; ?></p>
<?php
} else {
}
?>
<p>This is where the footer will go</p>
</body>
</html>
So far I have tried: $id = 1; and using a redirect to index.php. Neither worked, the later due to 'to many redirects'.
I would like to keep the code I have used so far rather than redoing it again, so if you can help, please give me advice on this code.
Thanks
All you need to do is to set the id to 1 (or what ever your home page is) if there aren't any id passed in the url. No need for the if-statement.
Here's how it can be done using the null coalescing operator:
$newArticle = new Article;
// Sets the id to what $_GET['id'] is set to if it exists, otherwise, set it to 1
$id = $_GET['id'] ?? 1;
$data = $newArticle->fetch_data($id);
?>
<p><?php echo $data['bodytext'] ?? 'Page not found'; ?></p>
I also added in how you can show "Page not found" if $newArticle->fetch_data($id) didn't return anything.
I am trying to make a website and it's almost completed but I want to add a view counter so when someone visit the page it count the view and save it into the database.
My script is working fine but the problem is that it continue view count even visitor is viewing anyother page
My pages url show like this
pictures.php?ID=13
I have added this PHP code in *count.php*
<?php
session_start();
if (isset($_SESSION['views'])){
$_SESSION['views']++;
} else {
$_SESSION['views'] =0;
}
//echo $_SESSION['views'];
?>
Page *views.php*
<?php
session_start();
if (isset($_SESSION['$post_id'])){
$_SESSION['$post_id']++;
} else {
$_SESSION['$post_id'] =0;
}
//echo $_SESSION['views'];
?>
<?php
echo "<hr><div align=\"center\">";
echo $_SESSION['$post_id'];
?>
<?php
$save = $_SESSION['$post_id'];
$con=mysqli_connect("localhost","root","123","user");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_query($con,"UPDATE save_data SET Views='$save' WHERE ID='$page_id'");
mysqli_close($con);
?>
And added this line in Pictures.php where I want to show and count visits
<?php include("views.php"); ?>
Problem:
When someone visits page pictures.php?ID=8 it will show him page view 1 and save this view in database where ID=8, when he visit page pictures.php?ID=12 it will show him view 2 and save this 2 in database where ID=12. My point is that it is continuously counting instead of each page view.
Thanks in advance
Here is Pictures.php
<?php
include("connection.php");
if(isset($_GET['ID'])){
$page_id = $_GET['ID'];
$select_query = "select * from save_data where ID='$page_id'";
$run_query = mysql_query($select_query);
while($row=mysql_fetch_array($run_query)){
$post_id = $row['ID'];
$post_title = $row['Title'];
$post_image = $row['Name'];
?>
<h3>
<a href="pictures.php?ID=<?php echo $post_id; ?>">
<?php echo $post_title; ?>
</a>
</h3><center>
<form id="search-form" action="javascript:void(0);">
<input type="text" id="dimen" name="dimension" />
<input type="submit" value="Resize" Onclick ="splitString()"/></form>
<div id="sizet">
Type size like 200*300 in box
</div></center>
<div id="img"><img id="myImage" src="uploads/<?php echo $post_image; ?>" /></div>
<?php } }?>
<center>
<div id="postdetails">
<?php include("posted_by.php"); ?></center>
</div>
<?php include("views.php"); ?>
<html>
<link href="css/Pictures.css" rel="stylesheet" type="text/css">
<body>
<head>
<script type="text/javascript">
function splitString()
{
var myDimen=document.getElementById("dimen").value;
var splitDimen = myDimen.split("*");
document.getElementById("myImage").width=splitDimen[0];
document.getElementById("myImage").height=splitDimen[1];
}
</script>
</head>
</body>
Variables inside of single quotes are not evaluated, so regardless of whether $post_id is 8 or 12, $_SESSION['$post_id'] is setting the key named literally $post_id, rather than the key named 12 or 8. Variables are evaluated inside double quotes, so $_SESSION["$post_id"] would work, but the simplest and best way is to use $_SESSION[$post_id] instead.
Additionally, using $_SESSION here is probably not what you want to do. $_SESSION will be different for every user who visits the site, so when a new visitor comes to the site, it will start over with a count of 1. What you probably want to do is load the views value from the database, add one to it, and then save it back to the database. $_SESSION is for keeping data that is specific to a certain user.
Try to use structure like this
$_SESSION['view'][{resource_name}_{resource_id}]
E.g. for picture with id 8 it will be
$_SESSION['views']['picutures_8']++
I'm really struggling with something and wondered if anyone could spare a few moments to have a look at this code block.
The original line looked like this:
$home_collectionsx=get_home_page_promoted_collections();
This brought back all the promoted to homepage items and displayed them on the homepage. I however simply want to pull 1 item in using the same code and an array function, the id is 5 for this purpose so I thought adding =array(5) or (array (5)) would work - but it doesn't.
I'm hoping it's something simple, or something that I have missed or not written correctly.
<?php
if(!hook("EditorsPick")):
/* ------------ Collections promoted to the home page ------------------- */
$home_collectionsx=get_home_page_promoted_collections (array(5));
foreach ($home_collectionsx as $home_collectionx)
{
?>
<div class="EditorsPick">
<div class="HomePanel"><div class="HomePanelINtopEditors">
<div class="HomePanelINtopHeader">Editors Pick</div>
<div class="HomePanelINtopText">This is the editors pick of Asset Space...</div>
<div class="EditorsPicImage"><div style="padding-top:<?php echo floor((155-$home_collectionx["thumb_height"])/2) ?>px; margin-top: -24px; margin-bottom: -15px;">
<a href="<?php echo $baseurl_short?>pages/search.php?search=!collection<?php echo $home_collectionx["ref"] ?>" onClick="return CentralSpaceLoad(this,true);"><img class="ImageBorder" src="<?php echo get_resource_path($home_collectionx["home_page_image"],false,"thm",false) ?>" width="<?php echo $home_collectionx["thumb_width"] ?>" height="<?php echo $home_collectionx["thumb_height"] ?>" /></div>
</div></div>
</div>
</div>
</div>
<?php
}
endif; # end hook homefeaturedcol
?>
This is the function to the DB itself that the above code is connecting to…
function get_home_page_promoted_collections()
{
return sql_query("select collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width from collection left outer join resource on collection.home_page_image=resource.ref where collection.public=1 and collection.home_page_publish=1 order by collection.ref desc");
}
Any help would be hugely appreciated :-)
Many many thanks
Rich
That function doesn't take a parameter: get_home_page_promoted_collections()
You want something like:
$home_collectionsx=get_home_page_promoted_collections(5);
And:
function get_home_page_promoted_collections($id=null)
{
$filterClause = '';
if(!is_null($id))
{
//to only return this id
$filterClause = ' AND collection.ref = '.intval($id);
//to get all but that id
$filterClause = ' AND collection.ref != '.intval($id);
}
return sql_query("SELECT collection.ref,collection.home_page_publish,collection.home_page_text,collection.home_page_image,resource.thumb_height,resource.thumb_width FROM collection LEFT OUTER JOIN resource on collection.home_page_image=resource.ref WHERE collection.public=1 AND collection.home_page_publish=1".$filterClause." ORDER BY collection.ref DESC");
}
I am currently creating a CMS.
Currently I have.
* Saved my images in mysql as app_image
* Saved the images as a URL to where the images are located
But creating MY INDEX PAGE only displays my link as a broken URL.
my code for this page:
<?php
include_once('include/connection.php');
include_once('include/article.php');
$article = new article;
$articles = $article->fetch_all();
?>
<html>
<head>
<title>testing</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
CMS
<ol>
<?php foreach ($articles as $article) { ?>
<li>
<a href="article.php?id=<?php echo $article['app_id']; ?>">
<img src="<?php echo $article['app_image']; ?>" height"100" width"100">
<?php echo $article['app_title']; ?>
</a> -
<small>
Posted: <?php echo date('l jS', $article['article_timestamp'] ); ?>
</small></li>
<?php } ?>
</ol>
<br><small>admin</small>
</div>
</body>
</html>
Can anyone see how I have gone wrong?
Thanks.
OK, I have done simalar thing and it is working just fine.
The code looks similar, and looks fine by me, now, maybe the link indeed is broken (maybe you didn't input the right upload link in DB)
I would go step by step and check that link (check if it is the right link). (with /path/name.ext)
If it is some help here is my case:
I put in DB post_id,post_title,post_contents, post_link
than i get that info with:
$query = $db->prepare ("SELECT bla bla FROM bla bla ORDER BY id DESC")
$query->execute();
$query->bind_result(everything that is selected seperated with ",");
(including $link)
<?php
while($query->fetch()):
?>
<a href="single-post.html" title="">
<img src="../images/<?php echo $link; ?>">
</a>
<?php
}
?>
NOW, the trick I did (to avoid problem is that i put inside DB only the name of file, the upload path is stored directly in HTML ("../images/")
Your code looks similar, and I think it should work, I think the problem is with link.
Var dump can come to the rescue here. Try this to see what the array key values should be set to for each of the elements in $article.
<?php foreach ($articles as $article) { ?>
echo '<pre>'; //just makes it a bit easier to read
var_dump($article); exit;
I have a list of names in a database that i want to display one by one
(also for bonus points, another column in the database is a Boolean value for if a task is completed or not. if this is true i want the css content box background to be green instead of red.)
so how can i select a name from row one, put it to a PHP variable, then select the value from the "Name" column in row 2 and put that to another PHP variable or the next item in the array?
thanks for any help!
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="mngPWinCSS.css"/>
</head>
<body>
<?php
$dsn ='mysql:host=****.******.com;dbname=o****_**n';
$username='********';
$password ='******';
mysql_connect('localhost',$username,$password);
$query=mysql_query("SELECT Name FROM CLOAS_Team LIMIT 0,1");
$bob="dkajfk";
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 60; URL=$url");
$com[1]="i";
$com[2]="i";
$com[3]="i";
$com[4]="i";
$com[5]="i";
$com[6]="i";
$name=mysql_fetch_array($query);
?>
<div id="content">
<img src="logjpg.JPG" alt="Smiley face" height="50" width="200">
<h3>CLOAS Tracker</h3>
</div>
<div id="Content">
<?php
?>
<div id="complete">
<h3names>
<?php
echo $name['Name'];
?>
</h3names>
</div>
<div id="incomplete">
<h3names>Name2</h3names>
</div>
</div>
</body>
</html>
First you need to change your SELECT query to select all of the rows that you wish to display, perhaps by taking off the LIMIT clause. Something like this;
$result=mysql_query("SELECT Name FROM CLOAS_Team");
(This will get you all of the names in your table.)
Next, you need to loop through the results you got from this query, like so;
$names = array();
while($row = mysql_fetch_assoc($result))
{
$names[] = $row['Name'];
}
This will put them into the array $names for you, which you can then work with. Instead of putting them into the array, you might want to output them immediately, perhaps like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div>
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>
However, you have many more errors in your code. Such as;
You can't just invent html elements called <h3names>
I doubt that you want to set the id attribute to 'incomplete'. An id should be unique, I expect you should be putting this in as a class (class = "incomplete")
I don't think your line header("Refresh: 60; URL=$url"); will do anything as your headers have already been sent to the page. If you want this line to work, it needs to be right at the top, BEFORE any output has been sent to the browser.
And for the bonus point, include the 'Completed' field in your query (if that is what it is called) and use this to add a style to each <div> element that you display in your loop. So your query might become;
$result=mysql_query("SELECT Name, Completed FROM CLOAS_Team");
And your loop would now be like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div style = "background-color:<?php echo $row['Completed'] == true ? 'green' : ' red'; ?>">
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>