I have two tables in a database, sight_country and sightseeing. I am inserting the ID of the country field from the sight_country table to s_country field of the table sightseeing. In php I am showing country field values from sight_country in a CSS drop-down menubar.
the code is
<li class="menu-item-has-children">Sightseeing
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
</li>
When click on link of county value then I am showing all sightseeing data from the table sightseeing in php page.
the code is
$sql = "select * from `sightseeing` where `s_country` ='$id'";
$res = mysql_query($sql);
$rec = mysql_fetch_array($res);
the country may have two or more related sightseeing data, so I am displaying sightseeing titles from the sightseeing table in a sidebar menu in my PHP page.
the code is
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if( mysql_num_rows($rec_st) > 0)
{
while($res_st = mysql_fetch_array($rec_st))
{
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>
when I click link of stitle I want to show it's related sightseeing data in same page. How it can be done?
I am assuming that;
The whole script is on one page (sightseeing.php), which varies depending on any GET variables (variables in the URL).
Originally the page just displays the first menu. Then when u click a country, you are sent again to sightseeing.php. Now also with ?id=* which shows also a second list, containing the list of sightseeing relevant to the country selected.
You have a field called 'id' in your sightseeing table that has the unique sightseeing id.
To now additionally show details of the sightseeing selected (clicked by user);
Modify the links in the second list. rather than:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]'>".$res_st['stitle']."</a></li>";
Write:
echo "<li><a href='sightseeing.php?id=$res_st[s_country]&ss_id=$res_st[id]'>".$res_st['stitle']."</a></li>";
Now when u click one of the links and are sent back to to sightseeing.php you will also have another get variable GET['ss_id'] (which has the id of the sightseeing that you want to view).
You can use this variable to pull the relevant details of the sightseeing.
$sightSeeingId = $_GET['ss_id'];
$sql3 = "select * from `sightseeing` where `id` ='$sightSeeingId' LIMIT 1";
$res3 = mysql_query($sql3);
$sightSeeingData = mysql_fetch_array($res3);
check that it has data and print it out
if(!$res3) die(mysql_error());
if(mysql_num_rows($res3) > 0){
echo "Sight Seeing id:" . $sightSeeingData['id'];
}
As a side note you should be aware that mysql_* functions are outdated and your code is vunerable to sql injection, see here;
GET parameters vulnerable to SQL Injection - PHP
You could give your second link a second parameter. Give the first parameter a unique name
<ul class="sub-menu">
<?php
$qry_st = "select * from `sight_country` limit 5";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while ($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$res_st[id]'>".$res_st['country']."</a></li>";
}
}
?>
</ul>
Then add a second parameter when generating the second link:
<ul class="st_lnks">
<?php
$qry_st = "select * from `sightseeing` where s_country = '$id'";
$rec_st = mysql_query($qry_st );
if ( mysql_num_rows($rec_st) > 0) {
while($res_st = mysql_fetch_array($rec_st)) {
echo "<li><a href='sightseeing.php?idCountry=$idCountry&idSightseeing=res_st['id']'>".$res_st['stitle']."</a></li>";
}
}
?>
</ul>
Related
I have a blog page. I want that when a user Clicks on "All Posts", it should show all posts from Blog. And when someone clicks on a category like "Catering", it should show the posts from "Catering" category only.
Here is what i have did so far
<div class="blog-nav">
<ul class="nav">
<li><a>All Posts</a></li>
<?php
$SelectBlogcats = mysqli_query($con, "SELECT * FROM blog GROUP BY cat_blog");
while($row = mysqli_fetch_assoc($SelectBlogcats)){
$Cat_query = mysqli_query($con,"SELECT * FROM cat_blog WHERE
id=".$row['cat_blog']);
$main_cat = mysqli_fetch_assoc($Cat_query);
?>
<li><a><?php echo $main_cat['bcat_name'];?></a></li>
<?php } ?>
</ul>
</div>
Tested and working answer
<?php
$cat_id=0;
$condition="";
if(isset($_GET['cat'])){
$cat_id=$_GET['cat'];
$condition="where cat_blog=$cat_id";
}
$SelectBlogcats = mysqli_query($con, "SELECT * FROM blog GROUP BY cat_blog");
while($row = mysqli_fetch_assoc($SelectBlogcats)){
$Cat_query = mysqli_query($con,"SELECT * FROM cat_blog WHERE
id=".$row['cat_blog']);
$main_cat = mysqli_fetch_assoc($Cat_query);
?>
<li>
<h2><?php echo $main_cat['bcat_name'];?></h2>
</li>
<?php } ?>
Some semi-pseudo code which might help you accomplish your goals - wholly untested however.
The nested sql queries you have is not very efficient and it is highly likely that a simple join between the tables is what you really need.
Each hyperlink used to select a new / different category to display needs to set ( in this example ) a querystring parameter which can be used to filter records from the entire recordset.
<?php
/*
Hopefully a single query should suffice
joining the two tables rather than querying
separately.
Store the result of the query and use that to build
the nav menu and also display the content late
*/
$cat=!empty( $_GET['category'] ) ? $_GET['category'] : 'all';
# without seeing the schema this is a guess!
$sql='SELECT * FROM `blog` b
JOIN `cat_blog` cb on cb.`id`=b.`cat_blog`
GROUP BY b.`cat_blog`';
$res=$con->query( $sql );
$rs=$res->fetch_all( MYSQLI_ASSOC );
?>
With the data stored in an associative array you can proceed to generate the hyperlink menu where each link has a querystring variable set; ie: ?category=X
<div class="blog-nav">
<ul class="nav">
<li><a href='?category=all'>All Posts</a></li>
<?php
foreach( $rs as $row ){
$active = $cat==$row['bcat_name'] ? ' class="active"' : '';
printf( '
<li%2$s>
%1$s
</li>
',
$row['bcat_name'],
$active
);
}
?>
</ul>
</div>
The recordset can then be processed again to output the actual blog content paying attention to the variable $cat to dictate which records are to be displayed.
<!-- blog display - ALL records or the filtered subset -->
<div class="blog-content">
<?php
if( !empty( $rs )){
foreach( $rs as $row ){
// display only the filtered category or ALL records
if( $cat==$row['cat_name'] or $cat=='all' ){
/* output your blog content ..... */
}
}
}else{
echo 'There was an error fetching database records.';
}
$res->close();
?>
</div>
This question already has an answer here:
Struggling to output PHP array as unordered HTML list
(1 answer)
Closed 2 years ago.
I want to return some data from my table as navbar links, my table holds this information:
Then I coded this:
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "
<li><a href='contact.html'>".$row->name_link."</a></li>
<li><a href='about.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='news.html'>".$row->name_link."</a></li>
<li><a href='blog.html'>".$row->name_link."</a></li>
<li class='active'><a href='index.html'>".$row->name_link."</a></li>
";
}
?>
But this is wrong because it prints this as result:
And I want each item to be printed once and then another name_link appears.
So how can I do that?
Assuming the value from name_link column is the same of html file, then:
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "<li><a href='".$row->name_link.".html'>".$row->name_link."</a></li>";
}
?>
This would seem to be more like what you need - create one <li> per row returned from your table. (I've assumed href-link is the field in your table which should be used as the URL.)
<?php
$result = $db->query("SELECT * FROM topnav ORDER BY 'table_id' ASC");
while($row = $result->fetch_object()){
echo "<li><a href='".$row->href_link."'>".$row->name_link."</a></li>";
}
?>
What you were doing before is repeating your entire menu for every row in your table, which doesn't make any sense.
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>';
...
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?
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.)