get id from url in same page - php

I am using the toggle visibility function in order to display and hide divs in one page.
Clicking on this link News makes a div visible where all the current news are displayed from the database using this code.
<?php
include"scripts/connect_to_mysql.php";
mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");
$news="";
$sql=mysql_query("SELECT *
FROM `news`
ORDER BY date DESC");
$newsCount=mysql_num_rows($sql);
if ($newsCount>0) {
while($row=mysql_fetch_array($sql)){
$id=$row["id"];
$title=$row["title"];
$text=$row["text"];
$date=$row["date"];
$news.=' <table width="800" border="0">
<tr>
<td style="width:150px;">' . $date . '</td>
<td style="width:600px; overflow:hidden;"><a href="#?id=' . $id . '" onclick="toggle_visibility(\'news_det\');" style="color:#b19057;" >' . $title . '</a></td>
<td style="width:50px">...more</td>
</tr>
</table>
';
}
}else {
$news="No news available yet";
}
?>
The problem is that by clicking on the results, the div where the detailed news should appear, appears, but I can't get the data from the database.
I use this code the get the id sent from the previous links.
if(isset($_GET['id'])){
$newsid= preg_replace('#[^0-9]#i','',$_GET['id']);
$sql = mysql_query("SELECT * FROM news WHERE id='$newsid' LIMIT 1");
$newsCount1 = mysql_num_rows($sql);
if ($newsCount1 > 0) {
while($row = mysql_fetch_array($sql)){
$dettitle = $row["title"];
$dettext = $row["text"];
$detdate = $row["date"];
}
}
else {
echo "No news with that id";
exit();
}
}
What am I doing wrong?

How to get the variable with PHP?
Let's say you have a PHP page named people.php. Now you can call this page using the following URL:
people.php?name=Joe
With PHP, you will be able to get the value of the variable 'name' like this:
$_GET["name"]
So, you use documentation $_GET to find the value of a named variable. Let's try it in an example:
<html>
<head>
<title>Query string</title>
</head>
<body>
<?php
// The value of the variable name is found
echo "<h1>Hello " . $_GET["name"] . "</h1>";
?>
</body>
</html>

May be its because you are using '#' in link
Change href="#?id=' . $id . '" to href="?id=' . $id . '"
If you are not refreshing the page then you should use ajax for this.

Related

Using anchor tag send id to next page in php

I am showing a html table which is retrieving the data from mysql using php,the table has a column feedback, the elements in this column are hyperlinked, they direct you to program called feedback.php where a feedback form is there with candidate name and id retrieved from database and shown there , below is the code showing the data displayed on table page
echo " <header class='w3-container w3-black'>
<h1>TABLE Of Faculty Position Applicants :-</h1>
</header>
<form action = '' method = 'post'>
<table class ='w3-table w3-striped w3-border'>
<tr class ='w3-grey'>
<th>Candidate No </th><th>Candidate Name</th><th>Google Scholar</th><th>DBLP</th><th>CV</th><th>Feedback</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['candidate_no'] . "</td>";
echo "<td>" . $row['candidate_name'] . "</td>";
echo "<td><a href=" . $row['gs_link'] . " target='_blank'>Google Scholar</a></td>";
echo "<td><a href=" . $row['dblp_link'] . " target='_blank'>DBLP link</a></td>";
echo "<td><a href=" . $row['cv_link'] . " target='_blank'>CV</a></td>";
echo "<td><a href=" . $row['feedback_link'] ." id = ".$row[id]. " target='_blank'>Feedback</a></td>";
echo "</tr>";
}
echo "</table>";
echo "</form>";
now you can see this line :-
echo "<td><a href=" . $row['feedback_link'] ." id = ".$row[id]. " target='_blank'>Feedback</a></td>";
which I am using to pass the id to feedback.php
on feedback.php this query is written which requires id and retrieved id of the specific candidate using id which is passed to this page
$query="SELECT candidate_name FROM faculty WHERE id=$_POST[id]";
$query2="SELECT candidate_no FROM faculty WHERE id=$_POST[id]";
$result = mysqli_query($conn,$query);
$result1 = mysqli_query($conn,$query2);
$row= mysqli_fetch_assoc($result);
$row2= mysqli_fetch_assoc($result1);
$conn->close();
I want to know the way to pass id to feedback.php so that I can retrieve candidate name and id on that program and show it in form which I will display because in above program I am doing some mistakes or is using wrong way to pass the id to feedback.php , please help me if there is problem I will clarify , help me.
You can pass the id as a GET variable, making a link something like this:
feedback.php?id=123
Then on feedback.php you can catch the ID by using $_GET['id'] where id is the name of the variable and the return will be whatever is set in it, in our example it will be ID 123.
You can then run your query on these IDs.
Use hyperlink queries.
http://mywebsite?data=test
server. Queries
You can access them easily in php:
<?php
$data=$_GET["data"]
?>
use $_GET instead of $_POST
$query="SELECT candidate_name FROM faculty WHERE id=$_GET['id']";
$query2="SELECT candidate_no FROM faculty WHERE id=$_GET['id']";

php opening link duplicate content

So on my blog page I am using substr to display only a bit of the articles. The idea is once someone clicks one of the headings they will be sent to a page displaying the full article.
When i click the link i get the right heading in the url for example i click the 4th blog entry i will be redirected to index.php?id=4.The problem is that on this page the content remains the same, nothing changes. How do I get it so when i click the article I will be directed to a page containing only that article in full and no others. Thanks in advance guys this has been wrecking my head all day.
<?php
$dbinfo = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
$result = mysql_query($dbinfo) or die(mysql_error());
$return = '<p> Go Back To Content Page</p>';
if(mysql_num_rows($result) !=0):
while($row = mysql_fetch_assoc($result)){
echo '<div id="roundedbox"><h2>' . $row['title'] . ' </h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more</div>";
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
?>
The query you have in your code will return all the records for the table, what you want is the query to return the record based on the id parameter from the $_GET['id']. It's a good idea to make sure you're only using integers to prevent sql injection.
From your code you need to have a separate query to fetch the appropriate record using the $_GET['id'] parameter as follows...
//the parameter is set and it is an integer
if(isset($_GET['id']) && is_int($_GET['id'])) {
$blogId = (int)$_GET['id'];
$query = "SELECT blog_id, title, date, body FROM content WHERE blog_id='$blogId'";
// run query and get record data and output it
} else {
//code to return all records as list
$dbinfo = "SELECT blog_id, title, date, body FROM content ORDER BY blog_id DESC LIMIT 0, 3";
$result = mysql_query($dbinfo) or die(mysql_error());
$return = '<p> Go Back To Content Page</p>';
if(mysql_num_rows($result) !=0):
while($row = mysql_fetch_assoc($result)){
echo '<div id="roundedbox"><h2><a href="index.php?id=' . $row['blog_id'].$row['title'] . ' </a></h2>';
echo '<div id="date"><h5><p>' . $row['date'] . '</p></h5></div>';
echo substr('<p>' . $row['body'] . '</p>',0, 90)." .... "." read more</div>";
}
else:
echo '<p> UH OOH! THERE IS NO SUCH PAGE IT DOES\'T EXIST </p>';
echo $return;
endif;
}

adding a sort feature for a table data called from mysql

i am trying to sort data in my table called from mysql database,i know its basic but i seen to get confuse,i trying to put a href tags in the th tags but it seems they are not working,this is my code below
<?php
include'includes/connect.php';
$sql = mysql_query("SELECT * FROM customers")
or die(mysql_error());
echo "<table border='1' cellpadding='10'>";
echo "<tr>
<th><font color='Red'>Id</font></th>
<th><font color='Red'>First Name</font></th>
<th><font color='Red'>Last Name</font></th>
<th><font color='Red'>Address</font></th>
<th><font color='Red'>Phone Number</font></th>
</tr>";
while($row = mysql_fetch_array( $sql ))
{
echo "<tr>";
echo '<td><b><font color="#663300">' . $row['id'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['first_name'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['last_name'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['address'] . '</font></b></td>';
echo '<td><b><font color="#663300">' . $row['phone_number'] . '</font></b></td>';
echo "</tr>";
}
echo "</table>";
?>
i recommended to use some jquery lite plugins to sort a table
like :
tablesorter
table fixed header
stupid table
you need to use ORDER BY function
mysql_query("SELECT * FROM customers ORDER BY YOURFIELD ASC")
change YOURFIELD with the field you want
change ASC with DESC to change order
Documentation here
if you want to make your header sortable than you should try this.
"a href='yourPHP path&sortingHeader=id'> add this as hyperlink to your headers for ex:- Id
Note:-and you should also add a little bit code of javascript in your php header above the
sql query
$sortingHeader = $_GET["sortingHeader"];
$subquery = "";
if ($sortingHeader != null && $sortingHeader != ''){
subquery = "order by "+$sortingHeader;
}
$sql = mysql_query("SELECT * FROM customers"+$subquery );
try this.....hope it will resolve your problem.
if you want to make your header sortable than you should try this.
add this 'a href="yourPHP path&sortingHeader=id"' as href to your header for example ID
Note:-and you should also add a little bit code of javascript in your php header above the
sql query
$sortingHeader = $_GET["sortingHeader"];
$subquery = "";
if ($sortingHeader != null && $sortingHeader != ''){
subquery = "order by "+$sortingHeader;
}
$sql = mysql_query("SELECT * FROM customers"+$subquery );
try this.....hope it will resolve your problem.

PHP and MySQL content displaying out of order

While dynamically generating a page with different types of contents , the "post" content is appearing above the static content which is being generated.I want it the other way around. Does there appear to be anything in my code that would make this happen, or do you think the problem has something to do with my database? Thanks.
$query = "SELECT * FROM content WHERE pages LIKE '%$pageID%'";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)) {
// Display pages's static content
if ($row['type'] == "static") {
echo "
<h2>" . $row['tile'] . "</h2>
<content>" . $row['body'] . "</content>
";
}
// Display pages's posts
else {
echo "
<h2>" . $row['tile'] . "</h2>
<content>" . $row['body'] . "</content>
";
}
SELECT * FROM content WHERE pages LIKE '%$pageID%' ORDER BY type desc
Add this to the end of your query:
ORDER BY CASE WHEN type = 'static' THEN 0 ELSE 1 END

PHP/MySQL OnClick Update MySQL

Need to update a hitcount field in a MySQL table when a user clicks on a banner ad. Have the random ad display script working but can't figure out how to update the table when they click..assuming will have to pass the ID to Ajax but no idea how to approach it? Code is below:
include 'connection.php';
$query = "select * from ads where adtype = 'small' and status = 'yes' ORDER BY RAND() LIMIT 3";
$result = mysql_query($query) or die(mysql_error());
$num_results = mysql_num_rows($result);
if ($num_results !="0")
{
for($i=0;$i<$num_results;$i++)
{
$row = mysql_fetch_array($result);
$client = htmlspecialchars(stripslashes($row['client']));
$link = htmlspecialchars(stripslashes($row['link']));
$filename = htmlspecialchars(stripslashes($row['filename']));
$id = $row['id'];
echo "<tr>";
echo "<td>";
echo '<a href="';
echo $link;
echo '"><img src="thimg/';
echo $filename;
echo '" alt="';
echo $client;
echo '"></a>';
echo "</td>";
echo "</tr>";
}
}
Make the link point to a page which takes the ID of the ad as a parameter, something like click.php?id=the_id. Then that page can update the database, look up the link, and then you can use a header redirect to forward them on to the link. Make sure you don't output anything on that forwarding page though, or the redirect won't work.
This should get you what you need, without the need for javascript or ajax.

Categories