How do I pull variables from my Database? - php

I have a menu of Home About Blog Templates Contact.
There is a table in my database called Categories.
The items in my table are Home About Blog Templates Contact.
These five items are being pulled from my database by my header.php file for my website.
What I want is to be able to click on each category and it display a specific .php file I have.
for instance if I click home it will display my index.php
clicking about displays my about.php
clicking blog displays my blog.php file etc.
I am not sure how to go about this ?
I am following a tutorial on Udemy as a guideline but, I need further assistance.
This is the php code at the top of my header.php file
<?php
include("includes/config.php");
include("includes/db.php");
$query = "SELECT * FROM categories";
$categories = $db->query($query);
?>
This is the code that is pulling my menu from the database
<?php if(isset($_GET['category']) || strpos($_SERVER['REQUEST_URI'] , "index.php") === false) { ?>
<a class="" href="index.php">Home</a>
<?php }else { ?>
<a class="" href="index.php">Home</a>
<?php } ?>
<?php if($categories->num_rows > 0) {
while($row = $categories->fetch_assoc()){
if(isset($_GET['category']) && $row['id'] == $_GET['category'] ){ ?>
<a class="" href="index.php?category=<?php echo $row['id']; ?>"><?php echo $row['text']; ?></a>
<?php } else echo "<a class='' href='contact.php?category=$row[id]'>$row[text]</a>";
} }?>
Essentially, what is going on in this code is when I click home it displays my index.php file and the code in my index.php file display code from another table I have.
However, when I click the about blog templates and contact categories it will only display the contact.php file for each category I have.
Thus, when I click About Blog Templates Contact only the Contact.php file displays.
I need a starting point on how to build on this where I can tell the code if about, blog, templates, contact is click on then display it's respective file.
The question is how do I use the categories in my database as my variables.
I was thinking using else if statements such as else if categories is = about then display. about.php
else if categories is = blog then display blog.php
If there are any free resources or books I can use to figure this out it would be much appreciated.
EDIT
The Table has only is called categories
ID Text
1 About
2 Blog
3 Templates
4 Contact
There no data stored in the table. All the table structure is ID & Text. The Text is being displayed as the menu and each Text has its associated ID. To be clear the HOME button is apart of the menu but, its not in the category table. I hope this is enough information.

Where you have this in the loop's else block:
} else echo "<a class='' href='contact.php?category=$row[id]'>$row[text]</a>";
Note the href has contact.php for all links.
What I believe that your trying to achieve, based on the categories, is:
} else echo '<a class="" href="' . $row['text'] . '.php?category=' . $row['id'] . '">' . $row['text'] . '</a>';
...so that you set the link's file name to match the category name in the link?

Related

Using PHP conditional statements to switch HTML content on different pages

I'm a front-end developer who is somewhat familiar with but rarely uses PHP. I'm working on a personal project where I'm mostly just using includes to link PHP files together. Here is my overall basic page structure:
<?php include('header.php'); ?>
<?php include('pagetitle.php'); ?>
Page content goes here.
<?php include('footer.php'); ?>
On pagetitle.php, I have an <h1>,<h2> and background image relating to which page you're on.
My question is, how do I use conditional statements to put all the page titles/subheadings on pagetitle.php and have them switch depending on what page you're on? So for example, I want
<div id="about">
<h1>About</h1>
<h2>About page subheading</h2>
</div>
to show up on about.php, and
<div id="contact">
<h1>Contact Me</h1>
<h2>Contact page subheading</h2>
</div>
to show up on contact.php, etc. etc. ...but only using pagetitle.php on those pages.
The site isn't huge. It would have no more than 10 pages. Also, I do realize I can just use that page title segment on the respective page, but if possible, I want to try this out.
Thanks!
I would do something like this (not tested, but should work with few, if any, changes.)
(Everyone says that, right? :D):
<?php
/*
create a map that contains the information for each page.
the name of each page maps to an array containing the
(div id, heading 1, & subheading for that page).
*/
$pageinfo = array(
"about.php" => array ("about", "About", "About Page Subheading"),
"contact.php" => array ("contact", "Contact Me", "Contact Page Subheading"),
);
function printinfo($pagename) {
/*
This function will print the info for the current page.
*/
global $pageinfo;
$pagename = basename($pagename);
#make sure we have info for this page
if (!array_key_exists($pagename, $pageinfo) {
echo "<p><b>You did not supply info for page $pagename</b></p>";
return;
}
#we do have info ... continue
$info = $pageinfo[$pagename];
#let's print the div (with its custom id),
echo "<div id='" . $info[0] . "'>\n";
#print the headings
echo "<h1>" . $info[1] . "</h1>\n";
echo "<h2>" . $info[2] . "</h2>\n";
#close the div
echo "</div>\n";
}
?>
Then in each page where you wanted your div, you would place this code:
printinfo($_SERVER['PHP_SELF']);
Other:
This way is more flexible than the other ways, at the sacrifice of no conditional statements. (You specifically requested a solution that had conditional statements; however, in the interest of flexibility & maintainability, this example does not use switch statements or if statements.)
Because there are no conditional statements, there is less code to maintain. Granted, you have to setup the array with the information, but if you decided to change the <h2> to an <h3>, you would have to make the change at only one location, etc.
On your pagetitle.php you could do something like this
<?php
$scriptname = basename($_SERVER["PHP_SELF"]);
if ($scriptname == "about.php") {
echo "<h1>About Page</h1>";
}else if($scriptname == "contact.php"){
echo "<h1>Contact Us</h1>";
}
?>
You have to get the string after the domain name so you can use $_SERVER['REQUEST_URI']
$page = $_SERVER['REQUEST_URI'];
if ($page == "about.php") {
echo "<h1>About</h1>"."<br />";
echo "<h2>About page subheading</h2>"."<br />";
}else if($page == "contact.php"){
echo "<h1>Contact Me</h1>"."<br />";
echo "<h2>Contact page subheading</h2>"."<br />";
}
$page = $_SERVER['REQUEST_URI'];
switch($page){
case 'about.php':
echo "<h1>About</h1>";
echo "<h2>About page subheading</h2>";
break;
case 'contact.php':
echo "<h1>Contact Me</h1>";
echo "<h2>Contact page subheading</h2>";
break;
default:
echo "Invalid Request";
}

Display images from SQL database on a webpage

I'm trying to display images on a webpage, using file paths queried from an SQL database. However, when I follow the link I only see one image (the second in the SQL table) which is incorrectly displayed as an icon and the title. I think the issue is with the echo statement, so could someone have a look? Thanks.
This is the link I follow from the selection page:
<a href="index.php?spain_2014">
<img src="congost.jpg" alt="congost" id="congost">
</a>
Then it passes through the controller, index.php:
else if(empty($_SERVER["QUERY_STRING"]) !== true)
{
$gallery_name = $_SERVER["QUERY_STRING"];
$rows = query("SELECT * FROM {$gallery_name}");
render("gallery_template.php", ["title" => "Gallery", "rows" => $rows]);
}
And is rendered in the gallery template:
<div id="gallery_images">
<!-- Gallery -->
<?php
foreach($rows as $row)
{
echo("<img src=\"~/vhosts/final_project/public/galleries/" . $row["filepath"] . "/>");
print($row["title"]);
} ?>
</div>

Apply CSS class to clicked link from multiple links to same page, different sections

I have 3 links that all point to the same page but to different sections.
This is what the page sections look like, they are all similar- just read from different tables.
<h1>Networking</h1>
<?php
include 'inc/connect.php';
$data = mysqli_query($link, "SELECT * FROM networking WHERE id = 1")or
die(mysqli_error($link));
while($info = mysqli_fetch_array( $data )) {
echo nl2br($info['info']);}
?>
</article>
This is the part of the menu file that has a link to 'Networking'.
if ($page == 'system') {
$output .="<li><a href='system.php#networking' class='active'>Networking</a></li>";}
else{
$output .="<li><a href='system.php#networking'>Networking</a> </li>";}
All the page section are in a page called system.php.
In the head of system.php I have this line
<?php $page = 'system'; ?>
This so I can apply the css class 'active' to the active link.
The way it is now, when I click on any 1 of my same page different section link the the class 'active' is applied to all 3 menu items.
Is there a way that I can apply the class to only the clicked link?
I understand your problem, you could do something like this:
system.php?section=networking#networking
And do this at the top:
<?php $page = 'system'; $section=$_GET['section']; ?>
And then change your if statement to:
if ($page == 'system' AND $section == 'networking') {
Note: this is not the best solution, but uses your style, I hope this will help you!
you can try something like
<?php $page = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_FRAGMENT ); ?>
But there is an additionnal issue you will need to have in mind. When you are already on the page and using a link to get on another section, the page won't reload.
If you want to update the active class in that case, you need something in javascript to do that.
Maybe that question can help you jQueryMobile add click event to a button instead of changing page

make my current link active

Well lets try to explain am sorry about my english.
I have some xml files where I get the url from with some php scripts
everything goes right the only prob is i want to change the li BGcolor of the selected link like in css :active or giving only that link eg a class="current"
this below make dynamically the urls to the data
echo "<ul><li ><a href='?xml1=".$xmlGet."&link=".$link." '>".$slide->title."</a></li></ul><br/>";
with the above i get a list of links not only one like below as urls
http://localhost/html5/playerEnd/hoofdstuks.php?xml1=chapter_3733&link=1
http://localhost/html5/playerEnd/hoofdstuks.php?xml1=chapter_3733&link=2
http://localhost/html5/playerEnd/hoofdstuks.php?xml1=chapter_3733&link=3
etc etc
and it display as menu like this
link 1
link 2
link 3
link 4
etc etc
each link load a different data to my page when clicked so i want the one clicked to be active like an other color or something.
Use $_GET['link'] to find out which link has been clicked. Then add a class to the link which corresponds to this. You'll have to define the active class.
$linkID = $_GET['link'];
echo "<ul><li ><a href='?xml1=".$xmlGet."&link=".$link." '";
if ($linkID == $link) { echo " class=\"active\" "; }
echo ">".$slide->title."</a></li></ul><br/>";
And if you want the li to have the class (as asked in comments):
$linkID = $_GET['link'];
echo "<ul><li";
if ($linkID == $link) { echo " class=\"active\" "; }
echo "><a href='?xml1=".$xmlGet."&link=".$link."'>".$slide->title."</a></li></ul><br/>";

How to get the URL of the current page with the title and post it to Digg?

We have a Joomla site and have purchased a template from Gavick.
I need to change the code which is part of a template we have purchased.
If I hardcode the parameters associated to the Digg button as follows then I am taken to Digg's website submission link form and can add the details.
The current code in the template is as follows:
<?php if($this->template->params->get("icon2", 1) == 1) : ?>
<a href="<?php echo $this->template->params->get("icon2_link", ''); ?>"
class="social_icon" id="social_icon2" target="_blank">Digg</a><?php endif; ?>
What I need to do is get the URL of the current page along with the title and post this to Digg.
Never used Joomla but...
<?php if($this->template->params->get("icon2", 1) == 1) : ?>
<a href="http://digg.com/submit?Url=<?php echo $_SERVER["REQUEST_URI"]?>&title=<?=mainframe->getPageTitle()?>&no_mobile=1"
class="social_icon" id="social_icon2" target="_blank">Digg</a><?php endif; ?>
I don't know variable for title.

Categories