URL friendly from DB, php and .htaccess - php

can help me?
I've a site url like:
http://www.example.com/episodio/ID
ID = row number from Database.
I want to display the page title.
I've this:
.htaccess file:
RewriteEngine on
RewriteRule ^episodio/(\w+)/?$ episodio.php?id=$1
php file:
$sql = "SELECT * FROM "episodios" WHERE serie = ".$id." AND temporada = ".$i." ORDER BY "episodio" ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if ($row['episodio'] == 0) {
echo '<li><strong>Episodio doble:</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}else{
echo '<li><strong>Episodio '.$row['episodio'].':</strong> '.$row["nombre"].'<img src="http://www.example.net/img/play.png" class="img-play"></li>';
}
?>
<?php } ?>

You're using $_GET in your ID anywhere before this code?
I mean, $_GET['id'].
Most PHP versions (since 4...) doesn't catch the var just for its name.

I'd recommend getting all your variables from the DB first, and then printing out the HTML (perhaps from a template file). We assume that $row contains more than just an ID? Perhaps an "título del episodio", also?
if ($result->num_rows > 0) {
$episodios = array();
$x = 0;
while($row = $result->fetch_assoc()) {
$episodios[$x]['id'] = $row['id'];
if ($x == 0) {
$PageTitle = $row['title'];
}
$x++;
}
Then your template can use $PageTitle (from the first episode), and you can loop through the $episodios array to construct the links for the other episodes.

Related

PHP foreach echo prints “Symbols” as value

I fetched data dynamically from from MySQL using a drop down menu through Ajax which was successful but when I echo the array values, instead of giving me the list of emails it was showing just symbols.
Take a look at the image below:
From the Email List, those are the symbols that were echo out.
here is my php code
if(isset($_POST["confirm_no"])){
$d = $_POST['confirm_no'];
$query = mysqli_query($mysqli, "select * from jobseeker WHERE confirm_no LIKE '$d%'");
//Display list
if(mysqli_num_rows($query) > 0){
$row = mysqli_fetch_array($query);
foreach ($row as $r) {
$emailArr[] = $r["mails"];
}
$emails = implode(";", $emailArr);
echo $emails;
}else{
echo 'No email for this selection.';
}
}
And the jQuery
$(document).ready(function(){
$('#smode').change(function(){
var confirm_no = $(this).val();
$.ajax({
type:'POST',
data:"confirm_no="+confirm_no,
url:'get_email.php',
success:function(data){
$('#emaillist').val(data);
}
});
});
});
Why is it echoing out this symbols?
Rebuild your code as:
//Display list
if(mysqli_num_rows($query) > 0){
// You have many results - fetch them all iteratively
// use `fetch_assoc` to have ability to use `mails`
while ($row = mysqli_fetch_assoc($query)) {
$emailArr[] = $row["mails"];
}
$emails = implode(";", $emailArr);
echo $emails;
}else{
echo 'No email for this selection.';
}
You are fetching the data incorrectly, you only fetch the one row (the call to mysqli_fetch_array()
$row = mysqli_fetch_array($query);
foreach ($row as $r) {
$emailArr[] = $r["mails"];
}
Could be better written as
while( $row = mysqli_fetch_assoc($query)) {
$emailArr[] = $row["mails"];
}
Or...
$emailArr = mysqli_fetch_all($query, MYSQLI_ASSOC);
$emailArr = array_column($emailArr, "mails");

Mailbox in website

I have a "Contact us" form in my website with the mailto: function. The mail goes to my yahoo account. I want to implement a mailbox in my website such that i can receive those mail directly from the admin page and reply or delete them accordingly.
How I can do that?
I already have the template I need an explanation on the functions and dynamic content to be implemented.
This is the sample of my code .. I have change some variable name here ..
$sql = "SELECT * FROM request where user_id='$user'";
$result = $conn->query($sql);
$sr=1;
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$sender_id=$row["sender_id"];
$receiver_id=$row["receiver_id"];
$req=$row['req_id'];
$stat=$row['status'];
if($stat==0){
$status="Pending...";
}
elseif ($stat==1) {
$status="Accepted.";
}
elseif ($stat==2) {
$status="Rejected.";
}
$sql1 = "SELECT * FROM usertable1 where id='$sender_id' ";
$result1 = $conn->query($sql1);
if ($result1->num_rows > 0) {
// output data of each row
while($row1 = $result1->fetch_assoc()) {
$username=$row1['user_name'];
$email=$row1['email'];
}
}
$sql2 = "SELECT * FROM usertable2 where id='$receiver_id' ";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
$source=$row2['source'];
$destination=$row2['destination'];
}
}
you will get an idea from this so you can implement as per your requirement.

.htaccess rewrite rule page not found

If page name doesn't exist then I would like to show page not found instead of a blank white page.
.htaccess:
RewriteEngine On
RewriteRule ^([0-9a-z\-\_]+)?$ page.php?name=$1 [L,QSA,NC]
page.php:
$name = filter_input(INPUT_GET, 'name');
$result = $mysqli->query("SELECT * FROM pages WHERE name='$name'");
while ($row = $result->fetch_assoc()) {
echo $row['content'];
}
If some of you know a better way, please share it :)
You can change your PHP code like this:
$name = filter_input(INPUT_GET, 'name');
if ($result = $mysqli->query("SELECT * FROM pages WHERE name='$name'")) {
if ( $result->num_rows == 0 ) {
printf('404 / page Not Found');
exit;
}
else {
while ($row = $result->fetch_assoc()) {
echo $row['content'];
}
}
} else {
printf("Error: %s\n", $mysqli->error);
exit;
}
You can get the mysql num rows and make if it == 0 then show page not found before the while loop

Passing retrieved database records to a php file

system/article.php
<?php
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["articleTitle"];
echo $row["articleSummary"];
echo $row["articleContent"];
}
} else {
echo "0 results";
}
include 'template/homepage.php';
retrieves articles from the article table.
I have included the homepage.php which is supposed to act as a template.
template/homepage.php
<?php include 'template/common/header.php'; ?>
<h1>Article Title here</h1>
<p>articleSummary</p>
<?php include 'template/common/footer.php'; ?>
How do I now pass the retrieved data to the homepage.php to display it on the browser ?
Edit
smarber pointed me to
In the first file:
global $variable;
$variable = "apple";
include('second.php');
In the second file:
echo $variable;
which works. But how do I implement the same with my problem up top?
You may do that via GET, Session or Post; But why don't you simply and efficiently define a function and pass those variables to it, just for example:
function displayArticle($title, $summary, $content) {
displayHeader(); // maybe some concepts you've used in template/common/header.php
echo "<h1>$title</h1><p>$summary</p><div>$content</div>";
displayFooter(); // again, what you've provided in footer.php
}
Well then, you may do the following:
change the template/homepage.php file to:
<?php
include 'template/common/header.php';
echo "<h1>$articleName</h1>";
echo "<p>$articleSummary</p>";
include 'template/common/footer.php';
?>
and change the system/article.php to:
<?php
global $articleName;
global $articleSummary;
global $articleContents;
$sql = "SELECT articleTitle, articleSummary, articleContent FROM articles";
$result = $dbconnect->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$articleName = $row["articleTitle"];
$articleSummary = $row["articleSummary"];
$articleContents = $row["articleContent"];
include 'template/homepage.php';
}
} else {
echo "0 results";
}
However, It's so better to create a cleaner and more reusable code using some facilities you have in the programming language, like using functions and classes :)

Passing php variables through pages / sql

i have the following information displayed
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0)
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
?>
as you can see i have created a game_details page which will display that specific Game_id when the image is clicked
im having trouble understanding how to pull the data out from that game_id in sql on the other page.
here is my attempt on the game_details page
<?php
if (!isset($_GET['$game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id={$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['$game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
This attempt is giving me the "invalid category ID" error
Would appreciate help
There are a few issues with your code.
Let's start from the top.
['$game_id'] you need to remove the dollar sign from it in $_GET['$game_id']
Then, $row['$game_id'] same thing; remove the dollar sign.
Then, game_id={$game_id}' will throw a syntax error.
In your first body of code; you should also use proper bracing for all your conditional statements.
This one has none if (mysqli_num_rows($result) > 0) and will cause potential havoc.
Rewrites:
<?php
$my_query="SELECT * FROM games";
$result= mysqli_query($connection, $my_query);
if (mysqli_num_rows($result) > 0){
while ($myrow = mysqli_fetch_array($result))
{
$description = $myrow["game_description"];
$image = $myrow["gamepic"];
$game_id = $myrow["game_id"];
$gamename = $myrow["game_name"];
echo "<div class='cover'>
</div>";
}
}
?>
Sidenote for WHERE game_id='{$game_id}' in below. If that doesn't work, remove the quotes from it.
WHERE game_id={$game_id}
2nd body:
<?php
if (!isset($_GET['game_id']) || empty($_GET['game_id']))
{
echo "Invalid category ID.";
exit();
}
$game_id = mysqli_real_escape_string($connection, $_GET['game_id']);
$sql1 = "SELECT * games WHERE game_id='{$game_id}'";
$res4 = mysqli_query($connection, $sql1);
if(!$res4 || mysqli_num_rows($res4) <= 0)
{
while ($row = mysqli_fetch_assoc($res4))
{
$gameid = $row['game_id'];
$title = $row['game_name'];
$descrip = $row['game_description'];
$genre = $row['genretype'];
echo "<p> {$title} </p>";
}
}
?>
Use error checking tools at your disposal during testing:
http://php.net/manual/en/mysqli.error.php
http://php.net/manual/en/function.error-reporting.php
You want to be using $_GET['gameid'] as that's the parameter you passed.
You are calling for game_id when the link to go to game_details.php has the variable gameid. Either change the parameter in the link to game_id or call for gameid in your $_GET['$game_id'].
Also, as Fred -ii- said, take out the dollar sign in $_GET['$game_id']

Categories