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/>";
Related
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?
I am fetching data from database table and showing result in HTML table. What I want to achieve is that, there is image column in every row, and every image is fetched from its URL (stored in database). I am opening an image in new tab whenever it is clicked.
My question is that, how can I store url of image so that on the next page I don't want to show that real url?
The next page will look like this link:
www.example.com/full_size_image.php
and add
img tag
there to show that page
how can I store that specific image url when someone click on it?
<img src= "<?php echo $_SESSION['link'] ;">
My current code is :
while($result = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<tr>";
echo "<td>".$result['c']."</td>";
echo "<td>".$result['UserName']."</td>";
echo "<td>".$result['UserProblemKeyword']."</td>";
echo "<td> <a href ='".$result['UserProblemPicture']."' target='_blank'><img src='".$result['UserProblemPicture']."' height='62' width='62'> </a> </td>";
echo "</tr>";
}
Use a parameter via the GET Request?
So instead of directing to just /full_size_image.php direct to
/full_size_image.php?img=filename.png
then in your php code for /full_size_image.php
You can fetch the file name via
<?php
$img = isset($_GET['img']) ? htmlspecialchars($_GET['img']) : "";
if ($img == "") {
//img not set
}
//do whatever with the filename
Can anybody tell me how can I add a css class to this links when click on it?
This is my links :
echo '<li>Tutor</li>
<li>Institute</li>';
My problem is how I identify which link is clicked by users?
I assume you want to add a class to a link if it is the current link being viewed.
Since each link has a different GET parameter you could use that to identify it.
echo '<li><a '. (isset($_GET['tutor']) ? 'class="current"' : '') .' href="?tutor=link&subject='.urlencode($subject).'">Tutor</a></li>
<li><a '. (isset($_GET['institute']) ? 'class="current"' : '') .' href="?institute=link&subject='.urlencode($subject).'">Institute</a></li>';
Using a pure JavaScript solution you could do:
<a id="tutor" href="?tutor=link&subject='.urlencode($subject).'" onclick="changeClass(this)">Tutor</a>
function changeClass(link) {
if (link.id == "tutor") {
link.className = "current";
}
}
Edit:
You can identify which link was clicked by the link variable passed into the changeClass function. The link variable will include an id property which will tell you the identifier of the a tag.
you can achieve this using javascript or jquery
$(this).css('newclassname')
thats the example of jquery
updated
you can bind it to a function to get the hold of the clicked element
$('a').click(function() {
$(this).css('current');
alert($(this).html()); // this will pop up tutor if its clicked
});
This is my problem: I have an ecommerce website, and I would like my search results to come up with products that have their links avaiable either within the image of the product on the search page, or next to the image. My current code looks like this:
if($_GET['searchBox'] !='')
{
if(mysql_num_rows($searchresult)==0) {
echo 'Your search returned no results';
}
else
{
while ($row = mysql_fetch_assoc($searchresult))]
{
echo "<img src='".$row['image']."'/>".' '.$row['name'].' £'.$row['price'].' '.$row['ProductUrl'];
}
}
}
?>
I have spent ages trying to get an URL into the image area, but I can't make it work.
Please help!!
What the problem put image in to the anchor tag.
Have you tried like this
echo "<a href='".$row['ProductUrl']."'><img src='".$row['image']."'/></a>";
EDIT
echo "<img src='".$row['image']."'/>".' '.$row['name'].' £'.$row['price'].'
<a href="'.$row['ProductUrl']."'>".$row['ProductUrl']."</a>";
Hi
I am going to highlight a menu item according to the page that is reading currently, when user click on different page through the menu, that menu item will be highlighted, example is http://templates.joomlart.com/ja_pyrite/index.php?option=com_content&view=article&id=44&Itemid=53.
If I use PHP/jQuery to check the url and highlight the menu, it will be good if the url look like "http://example.com/contact", but the example above is bad.
If I don't going to check the url and highlight the menu item, could someone give me a idea/method that can be done with the same effect?
Thank you
I found this on 960 Development, been googling for a while for this, so happy when I finally found it!
<ul class="sub-nav" >
<?php
$full_name = $_SERVER['PHP_SELF'];
$name_array = explode('/',$full_name);
$count = count($name_array);
$page_name = $name_array[$count-1];
?>
<li><a class="<?php echo ($page_name=='where-to-buy.php')?'active':'';?>" href="where-to-buy.php">WHERE TO BUY</a></li>
<li><a class="<?php echo ($page_name=='about.php')?'active':'';?>" href="about.php">ABOUT US</a></li>
<li><a class="<?php echo ($page_name=='contact.php')?'active':'';?>" href="contact.php">CONTACT US</a></li>
It is working fully for me, get the pages I need to be "active" to be active and dosent active any of thoes who got the class when I'm in another page!
Take a look at it!
Edit:
Even if you got a (in this example) contact.php?person=John, it will "active" the contact.php link!
do something like this
<div id="nav_menu">
<?php
$currentFile = $_SERVER['REQUEST_URI'];
$pages = array(
array("file" => "/index.php", "title" => "Home"),
array("file" => "/about.php", "title" => "About Us"),
array("file" => "/schedule.php", "title" => "Schedule")
);
$menuOutput = "<ul>";
foreach ($pages as $page) {
$activeAppend = ($page["file"] == $currentFile) ? " id='active' " : "class='nav_button'";
$currentAppend = ($page["file"] == $currentFile) ? " id='current' " : "class='nav_button'";
$menuOutput .= "<li " . $currentAppend . ">"
. "<a href='" . $page["file"] . "' id='".$page["id"]."'>" . $page["title"] ."</a>"
. "</li>";
}
$menuOutput .= "</ul>";
echo $menuOutput;
?>
</div>
i hope you get the idea, i had this on stackoverflow a while ago but i forgot what was the question
edit:
here i finnally found the original question
In the HTML code you use to generate your navigation, add some PHP logic that will add a selected class to the button of the page that you are currently on. Then just add some CSS for the selected class.
Can you do something like this? It should select any link that points at the current page - so you can apply whatever you like to highlight it.
$('a[href="'+window.location+'"]').addClass('menu-highlight');
A good technique is to add a specific class or id attribute to body element and then style it in CSS. It requires minimum of server side programming and keeps all the presentation logic in CSS as it should be done.
<style>
.contact #contact { background:#000; }
...
</style>
<body class="contact">
<ul>
<li id="homepage">Homepage</li>
...
<li id="contact">Contact</li>
</ul>
...