I have a previous and next button, I do not know how to decrease and increase the id using the get function. Can anyone please help, thank you.
PREVIOUS BUTTON CODE
<?php
include('db.php');
//now execut Select statement on table
$title = $_GET['title'];
$SQL = mysqli_query($db, "
SELECT *
FROM imageverse
WHERE (
id = IFNULL((SELECT max(id) FROM imageverse WHERE id < id='".$title."'),0)
);"
);
while ($row = mysqli_fetch_array($SQL)) {
$date = $row['date'];
$title = $row['title'];
echo "<h3>Prev - <a href = 'bibleread_post1.php?title=" . $title . "'#top'>". $title ."</a></h3>";
}
// Close your database connection
mysqli_close($db_conx);
?>
NEXT BUTTON CODE
<?php
include('db.php');
//now execut Select statement on table
$title = $_GET['title'];
$SQL = mysqli_query($db, "
SELECT * FROM imageverse
WHERE (
id = IFNULL((SELECT min(id) FROM imageverse WHERE id > id='".$title."'),0)
);"
);
while ($row = mysqli_fetch_array($SQL)) {
$date = $row['date'];
$title = $row['title'];
echo "<h3><a href = 'bibleread_post1.php?title=" . $title . "'#top'>". $title ."</a> - Next</h3>";
}
// Close your database connection
mysqli_close($db_conx);
?>
Related
i have this php code that fetches images from the database using the userid, then displays all the images in a list form. im trying to paginate the images, in a limit of 5 items per page. but the code is showing only the first page, without a link to the other pages. here's my php code
<?php
include 'connect.php';
$Category = " ";
$query = "SELECT Img_dir, Caption, Category FROM images WHERE Category = '". $_REQUEST['Category'] ."' AND user_id = '". $_SESSION['user_id'] ."' LIMIT 0,5";
$result = mysqli_query($conn,$query);
while ($row=mysqli_fetch_array($result)){
$image = $row["Img_dir"];
$Caption= $row["Caption"];
$Category = $row["Category"];
echo "<dl>";
echo "<dd>$Category    <img src='base64_encode($image)' />   $Caption<dd>";
echo "</dl>";
}
//number of total pages available
$results_per_page = 10;
$number_of_results = mysqli_num_rows($result);
echo $number_of_pages = ceil($number_of_results / $results_per_page);
echo "<br>"; echo "<br>";
for($r=1;$r<=$number_of_pages;$r++)
{
?><?php echo $r." "; ?><?php
}
?>
You can try this:
Change your query (use prepare statments):
$query = "SELECT Img_dir, Category FROM images WHERE user_id = ? AND Category = ? ";
As for the structure of your data.
$results = [];
while ($row = $result->fetch_assoc()){
$key = $row['Category'];
if(!isset($results[$key])) $results[$key] = [];
$results[$key][] = $row['Img_dir ']; //['Category' => [Img_dir,Img_dir, ...]]
}
And your HTML. I would use a description list or dl as it has a nice place for the title:
foreach($results as $Category => $image){
echo "<dl>";
echo "<dt>$Category</dt>";
foreach($data as $row){
echo "<dd><img src='base64_encode($image)' /><dd>";
}
echo "</dl>";
}
Untested.
The order will probably be all wanky, so you can use ksort on it. Simply
ksort($results);
Before the foreach loops.
Cheers.
Hi how do i change the URL id if the id is above max id in the db?
If i get the id from a database and use it in a read more button to make people read the data in a new page with the id as different url query?
--The real question
How do i make sure that if there only are 4 news in the db that if you write etc
newsTest.php?id=5 in the browser the browser will not execute or go back to max page?
//read more button
Læs mere
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages >= $thisVar){
echo "alt er godt";
if(isset($_GET['id']) && !empty($_GET['id'])){
$id = $_GET['id'];
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;
$result = $dbCon->query($sql);
if ($result->num_rows > 0) {
$row = $result->fetch_object();
$id = $row->id;
$heading = utf8_encode($row->heading);
$subheading = utf8_encode($row->subheading);
$description = utf8_encode($row->description);
$created = $row->created;
$author = utf8_encode($row->author);
$output .= $id . "<br>" . $heading . "<br>" . $description;
};
// udskriv output til bruger
echo $output;
};
} else {
$_GET['id'] = $total_pages ;
echo "nothing";
}
?>
I am very new to php
Check if total_pages is less than id then show show total_pages id.
<?php
include_once 'includes/db.php';
$sql1 = "SELECT COUNT(id) AS total FROM rock_news ";
$result1 = $dbCon->query($sql1);
$row1 = $result1->fetch_assoc();
if(isset($_GET['id']) && !empty($_GET['id'])){
$total_pages = $row1["total"];
$thisVar = $_GET['id'];
if($total_pages < $thisVar){
$thisVar=$total_pages
$id = $thisVar;
// mod sqlinjection
$id = $dbCon->real_escape_string($id);
$sql = " SELECT id, heading, subheading, description, created, author FROM rock_news WHERE id = " . $id ;
Hi I am building a blog using html and php and have run into a problem with my sql. In my blog I would like to show all the comments that have been put in by users in the comments section that have the same article ID. In my database I am saving these parameters via $_POST and a query ID, ArticleID, Comments. However only the last comment that has been inserted in the database with that articleID is showing up.
this is the code that I am using. Can anyone help me please?
if(isset($_POST['submit']))
{
$comment = htmlentities($_POST["comment"]);
$articleID = $_GET['artId'];
$query = "INSERT INTO tbl_comments (comment, ArticleID) VALUES ('$comment', $articleID)";
$result = mysqli_query($connection, $query) or die("Error in query: ". mysqli_error($connection));
}
$query1 = "SELECT * FROM tbl_comments WHERE ArticleID = $artId";
$result1 = mysqli_query($connection, $query1) or die("Error in query: ". mysqli_error($connection));
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
if(isset($comment))
{
echo "<div class='comments'>";
if (isset($comment))
{
echo "<div class='commentName'>";
echo $comment;
echo "</div>";
}
change
while($row = mysqli_fetch_assoc($result1))
{
$articleId = $row['ArticleID'];
$comment = $row['comment'];
}
to:
$comment = '';
while($row = mysqli_fetch_assoc($result1))
$comment .= $row['comment'] . '<br/>';
I have a multiple select list (links) which posts values to $links. I then want to run a query on table 'link' returning records that match values in $links. I am using the following code, but not getting any results:
<select name="links[]" size="9" multiple="multiple" id="links">
<?php
$query = mysql_query("SELECT * from link ORDER BY link_title ASC");
for($i=0;$i<mysql_num_rows($query);$i++) {
$row=mysql_fetch_assoc($query);
?>
<option value="<?php echo $row['link_pk']; ?>"><?php echo $row['link_title']; ?></option>
<?php
}
?>
</select>
And the submit code:
$author_pk = $_GET['author_pk'];
$title = $_POST['title'];
$topic_introduction = $_POST['topic_introduction'];
$selected_topic = $_POST['selected_topic'];
$links = $_POST['links'];
$majors = $_POST['majors'];
$majors_string = implode(",", $majors);
$sub_discipline = $_POST['sub_discipline'];
if(isset($_POST['submit'])){
$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";
$result_links = mysql_query($query_links, $connection) or die(mysql_error());
while ($row_links = mysql_fetch_assoc($query_links)){
$topic_links = array();
$topic_links[$row_links['url']] = $row_links;
} if($result_links){
$topic = $topic_introduction . '<p>' . $topic_links;
$query = "INSERT INTO topic (topic_pk,title,topic,majors,sub_discipline_fk,author_fk,created)
VALUES ('','$title','$topic','$majors_string','$sub_discipline','$author_pk',NOW())";
$result = mysql_query($query, $connection) or die(mysql_error());
if($result){
$message = "- The topic '" . $title . "' has been created";
}
}
}
This line is wrong:
while ($row_links = mysql_fetch_assoc($query_links)){
... because $query_links is actually a string (your SQL query). You should use $result_links, instead.
Also, the $query_links string isn't being defined correctly. You have to use the same delimiter at the end of a string literal that you use at the beginning. Instead of:
$query_links = "SELECT * FROM link WHERE link_pk IN ('.implode(',',$links).')";
Try:
$query_links = 'SELECT * FROM link WHERE link_pk IN (' . implode(',', $links) . ')';
I have a large database of venues - and I would like to display this data in one page that would only change in some sort of an attribute to the id: (Ex: venues.php?id=1, which would get all the data from row #1.)
Edit: Okay, I updated the code and this is what it looks like now:
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>
And upon going to venues.php?id=1 I get this error:
Parse error: syntax error, unexpected T_LOGICAL_OR in
/home/nightl7/public_html/demos/venues/venues.php on line 8
Do you mean something like:
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".(int)$id) ;
In order to "pass" the id into your url "venues.php?id=1"
You need to use a hybrid html/php form with method=get.
You can see an example html form here: w3schools html forms
This is what I would do:
print '<form name="input" action="venues.php" method="get">';
print 'Venue: <select name = "id">';
$con = mysql_connect("","","");
mysql_select_db($dataBase);
if (!$con){die('Could not connect: ' . mysql_error());}
else {
$opt = array();
$optVal = array();
$i = 0;
$sql = "Select * from venues";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result))
{
$opt[$i] = $row['VenueName'];
$optVal[$i] = $row['VenueID'];
print "<option value='$optVal[$i]'>$opt[$i]</option>";
$i++;
}
}
mysql_close($con);
print '</select><br />';
print '<input type="submit" value="Submit" />';
print '</form>'
This will give you a form that will give you a drop down list of all your venues and once a venue is selected will direct you to the venues.php page with the respective id.
at the top of your venues,php page just use
$id = $_GET['id'];
This assigns the id number to the variable $id and then you can use this "select"
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) ;
To collect your venue name from your database using the id supplied in the form.
Good Luck :)
<?php
mysql_connect("localhost", "", "") or die(mysql_error());
mysql_select_db("") or die(mysql_error());
$id = (int) $_GET['id'];
$data = mysql_query("SELECT * FROM venues WHERE id = ".$id) or die(mysql_error());
Print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
Print "<tr>";
Print "<th>Name:</th> <td>".$info['VENUE_NAME'] . "</td> ";
Print "<th>Address:</th> <td>".$info['ADDRESS'] . " </td></tr>";
}
Print "</table>";
?>