Loading more results from database using $_POST (or $_GET?) - php

I have a blog page "plugin" in my own little cms and it's displayed with return() on the index.php.
Part of the index.php:
$id = (isset($_GET['m_id'])) ? $_GET['m_id'] : 1; //menu id
$sql = "SELECT m_cim, m_tartalom, m_plugin
FROM menu
WHERE m_s_id = (SELECT s_id FROM statusz WHERE s_nev='aktiv')
AND m_id = ".$id;
$eredmeny = mysql_query($sql);
if (#mysql_num_rows($eredmeny) == 0) {
$tartalom = "<h1>404!</h1>\n";
}
else {
$sor = mysql_fetch_assoc($eredmeny);
$tartalom = "<h2>{$sor['m_cim']}</h2>
<span class=\"tart\">{$sor['m_tartalom']}</span>\n";
if(!empty($sor['m_plugin'])){
$tartalom.=include("./modul/{$sor['m_plugin']}");
}
}
<section id="content">
<?php print $tartalom; ?>
</section>
The posts come from a database and the news.php is included in the index.php.
This is the "plugin" news.php
$aktiv="(SELECT s_id FROM statusz WHERE s_nev='aktiv')";
$sql = "SELECT hir_id, hir_cim, hir_tartalom, hir_ido
FROM hirek
WHERE hir_s_id=".$aktiv."
ORDER BY hir_id DESC";
$eredmeny = mysql_query($sql);
$kimenet = "";
while ($sor = mysql_fetch_assoc($eredmeny)) {
$kimenet.= "<article class=\"hirek\">
<h3>{$sor['hir_cim']}</h3>
<span class=\"hido\">{$sor['hir_ido']}</span>
<p class=\"htart\">".substr(strip_tags($sor['hir_tartalom']),0,200)."</p>
Tovább...
</article>\n";
}
return $kimenet;
If I use a php pagination, I only saw, that the page number is posted via the ‘GET’ method, but in that case, I use the GET method for the menu id, and when I want to post something else with get, the result will be the menu item with the actual id. Is it possible to use $_GET method for this?
I think this technique can solve my problem, but I don't know if it's outdated/not-so-good/do-not-use-it or not.
When I find somewhere a solution for my actual problem, somebody always says that "do not use it, because..." "it's not the best idea, because..." "this is not the best solution,because.."
Can I trust in this?

Related

PHP/HTML,how can I hide the current article from sidebar if already open?

I would like to hide the article from the sidebar if it is already open, but I don't know how it can be done. I'm a newbie, sorry about that.
I am using MySqli select random to show 8 articles in a sidebar.
The articles are saved in a variable and then I echo them on a different page.
Can anyone help me out or tell me how should I do this. What kind of code to use. Thank You.
Here is the article code.
case'article':
$get_article = mysqli_query($conn, "SELECT title,link,excerpt,description_1,description_2,description_3,description_4,description_5,description_6,description_7,description_8,description_9,description_10,description_11,description_12 from `articles_".LANG."` where link = '".$_GET['article']."'") or die(mysqli_error($conn));
if(mysqli_num_rows($get_article) != 1){
header("Status: 404 Not Found");
$page = '404';
}
else{
$row = mysqli_fetch_assoc($get_article);
$title = $row['title'];
$description = $row['excerpt'];
$content_url= 'http://www.example.com/'.$_GET['lang'].'/'.$row['link'].'/';
$meta_img = 'http://www.sub.example.com/'.$_GET['lang'].'/'.$row['link'] .'/1.jpg';
$files = glob('/home/site/sub.example.com/'.$_GET['lang'].'/'.$row['link'].'/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
$total = count($files);
for($i = 1;$i <= $total; $i++){
if(is_file('/home/site/sub.example.com/'.$_GET['lang'].'/'.$row['link'].'/'.$i.'.jpg')){
$picture = $i.'.jpg';
}
elseif(is_file('/home/site/sub.site.com/'.$_GET['lang'].'/'.$row['link'].'/'.$i.'.jpeg')){
$picture = $i.'.jpeg';
}
$pictures .= '<div class="article_box"><h3 class="top_img_description">'.$row['description_'.$i]. '</h3>'.'<div class="single_img_container"><img src="http://sub.example.com/'.$_GET['lang'].'/'.$row['link'].'/'.$picture.'"/></div></div>';
}
Here is the sidebar code.
$article_sidebar = mysqli_query($conn, "SELECT title,link from `articles_".LANG."`ORDER BY RAND() LIMIT 8") or die(mysqli_error($conn));
while($row2 = mysqli_fetch_array($article_sidebar)){
if(is_file('/home/site/sub.example.com/'.$_GET['lang'].'/'.$row2['link'].'/1.jpg')) {
$file='http://sub.example.com/'.$_GET['lang'].'/'.$row2['link'].'/1.jpg';
}
$sidebar_articles .= '<div class="article">
<div class="article_thumb">'.'<img src="'.$file.'"/>
</div>
<h3 class="feed_title">
'.$row2['title'].'
</h3>
</div>';
}
And here is the a screenshot of what I want.
"SELECT title,link from `articles_".LANG."`WHERE id <> ".$currentid." ORDER BY RAND() LIMIT 8"
Exclude the id of the current displayed element in your query. This is done for example by using <>. And don't forget to check the values before you put them in a mysql-query!
You can do this with javascript if want to be able to switch through articles at some point without the page reloading. Add a unique class to all articles on the right and the main article on the page (a unique ID perhaps) and compare this to the main section. If the two match then apply apply a hidden class or change it's display property to none.
If you want to do this server side and switching between articles causes the page to refresh, you should apply a similar approach to the above. Store some reference to the current 'main' article and wrap the sidebar articles in a foreach.
In this foreach, wrap each addition to the sidebar html in an if statement which compares the the current value with that of the main article.
eg.
if ( $currentArticleId !== $mainArticleId )
{
$sidebar_articles .= '<div class="article">
<div class="article_thumb">'.'<img src="'.$file.'"/>
</div>
<h3 class="feed_title">
'.$row2['title'].'
</h3>
</div>';
...

Create new page when creating post

Currently I'm making a blog out of scratch.
I already have a login page and a "create post page". But when I create a post I just store the information inside the database.
How can I make it that I create a new page for the blog post?
Something like: [sitename].com/post.php?id=1
Thanks for helping :)
Interesting question. Here I have an example on how you could do that (PDO). $_GET["id"] will take the ID from the URL. As example domain.com/post.php?id=[getid]
<?php
$id = $_GET["id"];
$sql = "SELECT * FROM `posts` WHERE `id` = $id";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo $row["title"].' '.$row["content"];
}
?>
And if you want to link them back to the homepage when the ID is not given, you could use this;
if (empty($_GET["id"])) {
header("Location: index.php");
}
For linking from the index to the posts you can use;
$sql = "SELECT * FROM `posts` ORDER BY `id` DESC";
$result = $conn->query($sql);
while($row = $result->fetch(PDO::FETCH_ASSOC)) {
echo '<a href="posts.php?id='.$row["id"].'">';
}
}
In your post.php file, you can load the content from a specific post using the id provided in your url.
<?php
$postid = $_REQUEST['id'];
$post = someSqlFunction($postid); // <-- Replace with database code
echo '<h1>' . $post['title'] . '</h1>';
So if you wanted to load the post with id 15, you simply go to [sitename].com/post.php?id=15
You can create a page for each and every blog post you create. When you create a blog post there must a submit or post, after submitting it goes to a php file where you write the code to store the contents of the blog post. In that very file you write some code to create html file (file programing) for blog post having particular id. And you add all the code of css,html,also contents and write to that file and save it as .html format(eg. BlogPost_1.html etc). You can store file names in database for later use.
I hope thats what you needn

Adding hyperlink to a table element, carrying its informations [PHP]

Being a huge PHP newbie I find myself stuck here.
I have an HTML table for a videogame store filled with elements taken from my database.
The point is, I want to be able to add a link to the game title. Moreover I want the link to direct to some "gamePage.php", a php page used for every videogame but of course showing different infos for each game (title, console etc).
The fact is that not only I can't add the hyperlink, but I have no clue on how to carry the videogame infos when I click on a link (even managing to add the link, all I would manage to do would be redirecting the user to a blank gamePage.php with no title).
This is the code I use to fill the table (the restore function fills my table):
<html>
<body>
<div>
<table width = "550px" height = "300px" border="2" >
<tr bgcolor="#5f9ea0">
<td>Title</td>
<td>Console</td>
<td>Genre</td>
<td>Price</td>
</tr>
<?php
$conn = #pg_connect('dbname=project user=memyself password=project');
function search(){
<!-- Work in progress -->
}
function restore(){
$query = "SELECT v.Title , c.Consolename , g.Genrename , v.Price
FROM vg_shop.videogame v, vg_shop.console c, vg_shop.genre g
WHERE v.Console=c.IDConsole AND v.Genre=g.IDGenre";
$result = pg_query($query);
if (!$result) {
echo "Problem with query " . $query . "<br/>";
echo pg_last_error();
exit();
}
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
}
}
<!-- some code -->
</body>
</html>
At first i tried to do this
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>",
$myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
But all I get is a white page, there's some syntax error I don't get.
And, even if it worked, I still can't carry at least the videogame PID through the gamePage link
so, you're managing to go to gamepage.php somehow.
So you need to add some sort of identifier to your link you that you could do some query on the gamepage.php by using that identifier to get info for that particular game.
while($myrow = pg_fetch_assoc($result)) {
printf ("<tr><td><a href='gamePage.php?id=%s'>%s</a></td><td>%s</td><td>%s</td><td>%s</td></tr>", $myrow['id'], $myrow['title'], $myrow['consolename'], $myrow['genrename'], $myrow['price']);
Note: I assume that you're picking $myrow['id'] from database as well.
now on your gamepage.php do following.
$id = $_GET['id'];
$sql = "SELECT * FROM vg_shop.videogame WHERE `id` = $id";
$result = pg_query($sql);
if($result){
$result = pg_fetch_assoc($result):
//...
// echo "Name: ".$result['Title'];
// other fields
}
$result will have all info about that particular game that was clicked, you can display all as you want.
Cheers :)

Using PHP (and maybe AJAX?) with jQTouch

I am having some difficulty using PHP with jQTouch. I am fairly
confident with JavaScript however my PHP skills are little to none.
I am creating an application for my final year project at University
what displays football rumours posted by different users. My problem
is as follows:
I have one screen that displays each individual rumour, using a while
loop in PHP I am able to get each rumour from the database and display
them correctly. However I want to be able to click on one rumour which
then displays this rumour in a different screen, along with options to
reply/share etc. However I do not know how to tell which rumour has
been clicked on.
Snippets of my code:
All rumours page:
<?php
$q1 = "SELECT * FROM tblrumours;";
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_assoc($r1) ){
?>
<a class="rumourTag submit" id="<?php echo $row1['rumourID']; ?>">
<div class='oneRumour'>
<div class='standardBubble'>
<p>
<?php
$userID = $row1['userID'];
$q2 = "SELECT * FROM tblusers WHERE userID = $userID;";
$r2 = mysql_query($q2);
while( $row2 = mysql_fetch_array($r2) ){
$username = $row2['username'];
$teamID = $row2['teamID'];
}
$q5 = "SELECT * FROM tblteams WHERE teamID = $teamID;";
$r5 = mysql_query($q5);
while( $row5 = mysql_fetch_array($r5) ){
echo "<img src='img/".$row5['teamPicture']."' alt=''
class='teamImg' />";
}
?>
<span class='username'>
<?php
echo $username;
?>
</span>
<br/>
<span class='rumourMsg'><?php echo $row1['rumourText']; ?></
span>
</p>
</div>
</a>
SINGLE RUMOURS PAGE:
<?php
$q1 = "SELECT * FROM tblrumours WHERE rumourID = 1;"; /* NEED
TO SELECT WHERE RUMOUR ID IS THE ONE THAT IS CLICKED */
$r1 = mysql_query($q1);
while( $row1 = mysql_fetch_array($r1) ){
?>..........
I have tried using Session variables, storing the ID's in an array,
creating a separate php file for the single rumour page, and all to no
avail. I am guessing I have to use AJAX in some way, but I have no
idea where to even begin. Any help is greatly appreciated!
Thanks!
If you need to click on a rumour to see more details about it, you could always output in the HTML a unique value used to reference that rumour in the DB.
e.g. have <span class='rumourMsg' id='rumourName'> where rumourName is a unique value stored in your database to reference that rumour. Then when a user clicks to see more details, you can make a request to the PHP page with that value and return the content.
e.g. rumourDetails?rumourName=uniqueRumourName
(make sure to escape all your data properly to avoid SQL injection vulnerabilities.)

PHP - How to Create Dynamic URLs?

I've scoured the web for a tutorial about this simple task, but to no avail. And so I turn to you helpful comrades. Here's what I need to do:
I have a MySQL database with an Events table. I need to create a PHP web page with a list of the Event titles, and each title must be a link to the full details of the Event. But I want to avoid having to create a static page for each event, primarily because I don't want the data entry volunteer to have to create these new pages. (Yes, I realize that static pages are more SEO friendly, but I need to forego that in this case for the sake of efficiency.)
I've seen PHP url syntax with something like this:
pagename.php?id=20
but I don't know how to make it work.
Any and all help greatly appreciated.
Thanks!
Kip
This is basic php. You would simply query the DB for the event details before the page headers are written and write the html accordingly.
The first thing I would ask you is if you know how to connect to your database. From there, you query based on the $_GET['id'] value and use the results to populate your html.
Not to be rude, but the question itself suggests you're new to PHP, right? So in order to provide a solution that works we might want to know just how far you got.
Also, you can rewrite your dynamic urls to appear like static ones using apache's mod_rewrite. It's probably a novice level thing if you're interested in "pretty" url's.
MODIFIED ANSWER:
In your loop you would use the id from the query result (assuming your primary key is id)...
while($field = mysql_fetch_array($result)) {
echo "<p class='date'>";
echo $field['month']." ".$field['day'].", ".$field['year'];
echo "</p>";
echo "<h3>";
echo ''.$field['event_name'].'';
echo "</h3>";
}
Then on somepage.php you would use the get var id to pull the relevant info...
$result = mysql_query("SELECT * FROM `calendar` WHERE `id` = '".mysql_real_escape_string($_GET['id'])."');
don't forget to look into mysql_real_escape_string() for cleaning entries.
It's wise to take extra care when you are using $_GETvariables, because them can be easily altered by a malicious user.
Following with the example, you could do:
$foo = (int)$_GET['id'];
So we are forcing here the cast of the variable to a integer so we are sure about the nature of the data, this is commonly used to avoid SQL injections.
lets say you have the php file test.php
<?php
$conn = mysql_connect("localhost", "root", "");
mysql_select_db("db", $conn);
$id = $_GET['id'];
$sql = "select * from table where id = $id";
$result = mysql_query($sql, $conn);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[0];
$content = $row[1];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
a dynamic page would be something like that..
Here is the pertinent code for extracting a list of events in November from a table named calendar, with each event having a link to a page called event.php and with the event's id field appended to the end of the url:
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"<a href='event.php?id=".$row['id']."'>".$row['event_name']."</a>"
;}
And here is the pertinent code on the event.php page. Note the row numbers in brackets depends on the placement of such in your table, remembering that the first row (field) would have the number 0 inside the brackets:
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];
}
?>
<html>
<head>
<title><?php echo $title ?></title>
</head>
<body>
<h1><?php echo $title ?></h1>
<p><?php echo $content ?></p>
</body>
</html>
This works for me, thanks to the help from those above.
$foo=$_GET['id'];
in your example $foo would = 20

Categories