Active menu class with php function - php

I'm not getting it done, I need to put a class on the output of the function.
Can you help, when the menu item is active there has to be put a class to the link.
This is the code:
function loadMenu()
{
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{ $last_parent = $row['page_name'];
echo "<a href={$row['link_name']}>{$row['page_name']}</a>";
}
}

function loadMenu()
{
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{
$last_parent = $row['page_name'];
$class = $row['active'] === true ? ' class="active"' : '';
echo "<a href=\"{$row['link_name']}\"{$class}>{$row['page_name']}</a>";
}
}
Also your

I'm not 100% sure about what's your issue.
So, if you aren't displaying anything, probably a MySQL issue.
1- Reference for Mysql (Look at OZ_'s answer) : How to put mysql inside a php function?
2- As mentionned by Beiller, you'll need a way to see if the link is active.
Solution A : Might need some adjustment according $row['link_name'] value.
$class = ($row['link_name'] == $_SERVER['REQUEST_URI'].)?' class="active" ' : '';
Solution B : Simply stick to CSS - http://www.echoecho.com/csslinks.htm
.mylink:active{ background-color:pink; }
3- Href needs quotes on around the URL.
also, I'm not a big fan of {} inside strings.
echo "". $row['page_name'] ."";

Thanks beiller for the help..
This is the function now:
function loadMenu(){
$result = mysql_query(" SELECT * FROM cms_page WHERE site_id =2 AND page_inmenu =1 AND page_active =1");
if ($DEBUG)
echo "<pre>$result</pre>";
while($row = mysql_fetch_array($result))
{ $last_parent = $row['page_name'];
echo "<a href={$row['link_name']}>{$row['page_name']}</a>";
}}
This is the call:
<div id="header_menu">
<? loadMenu (); ?>
</div>
And mysql has the following tables:
page_id
page_name
link_name
site_id
order_id
page_menu
page_inmenu
page_leftmenu
page_text
page_active

Related

Find and display certain strings from database with if and else

The php-code below finds and displays an external url as a link from a database. Sometimes there is a phonenumber instead of an url in that row. How can I manage to echo out the phonenumber without making it to a link. I guess it will work with if and else but I'm not finding out how to display it the right way. My code below:
<?php
$ticketurl = $row ['show_external_url'];
$query = mysqli_query( $connection,"SELECT *, DATE_FORMAT(`show_date`, '%W (%d/%m/%y)') as dateFormatted
FROM nhto_gigpress_shows
WHERE `show_date` >= CURDATE()
AND WEEKDAY(nhto_gigpress_shows.show_date) >= 5
ORDER By show_date" );
$row = mysqli_fetch_array( $query ); {
echo "<a href='".$ticketurl."'>BOOK</a>";
}
?>
Try to check whether the column has any data or not and use condition
inside the while loop:
if(empty($row['url'])){
echo $row['phone'];
}
else{
echo 'BOOK';
}

Display album using PHP and SQL

I have a question which is very similar to this one: Display Album with Photos (PHP), however I need a bit of help in applying the code to my situation.
I have two tables, which are related by the column AlbumID:
Album: AlbumID, name, date(optional)
Image: ImageID, name, imageURL, AlbumID
My PHP code so far:
function select_galleryimage($sql,$a_id, $img_id) {
include 'connect.php';
$result = $conn->query($sql);
if ($result->num_rows > 0);
while ($row = $result->fetch_assoc()) {
echo "<div id='".$row['AlbumID']."'>\n
<a class='$a_id' href=".$row["imageURL"]." target='_blank'>
<img src=".$row["imageURL"]." alt='".$row['name']."'>
</a>\n
</div>\n";
}
}
where $sql = SELECT * FROM album JOIN image ON album.AlbumID=image.AlbumID'
$a_id = 'lightbox' and $img_id = ''
The issue that I am having is how to create a loop which creates the divs into which the loop with the images is inserted, so as the end result is something like this:
<div id="album1" style="display:none">
<h1>Gallery</h1>
<a class="lightboxX35" href="media/Photos/_MG_7732.jpg"><img src="media/Photos/_MG_7732.jpg"></a>
<a class="lightboxX35" href="media/Photos/_MG_7508.jpg"><img src="media/Photos/_MG_7508.jpg"></a>
</div>
Gallery
Thank you all in advance for any help provided.
Somehing like this?
if ($result->num_rows > 0) {
$current_album = null;
while ($row = $result->fetch_assoc()){
if ($current_album <> $row['AlbumID']) {
if (!empty($current_album))
echo "</div>\n";
echo "<div id='".$row['AlbumID']."'>\n";
echo "<h1>Gallery</h1>\n";
$current_album := $row['AlbumID'];
}
echo "<a class='$a_id' href='".$row["imageURL"]."' target='_blank'><img src=".$row["imageURL"]." alt='".$row['name']."'></a>\n";
}
echo "</div>\n";
}
I haven't tried it on a server so may not be the correct synthax...
With more than a single album you have to sort the SQL by album using ORDER BY then echo the DIV fo every new album. To achieve this assign a variable $current_album = null; outside the while loop then check for changes inside.

PHP/SQL fetch query data

I'm having a problem in this simple SQL/PHP query...
<?php
$course=$row['course'];
include('../db.php');
$cat=$row['cat'];
$result = mysql_query("SELECT * FROM question WHERE course='$course' AND cat='$cat'");
while($row = mysql_fetch_array($result))
{
echo $row['question'].'?<br>';
$qid=$row['qid'];
echo '<input type="hidden" name="qqqq[]" value="'.$qid.'" />';
echo '<select name="answer[]">';
echo '<option>Select Answer></option>';
$resultik = mysql_query("SELECT * FROM choices WHERE question='$qid' ORDER BY RAND() LIMIT 4");
while($rowik = mysql_fetch_array($resultik))
{
echo '<option>';
echo $rowik['opt'];
echo '</option>';
}
echo '</select><br><br>';
}
?>
Basically, this is a online examination. I want to display all the questions if the student will login. And the questions will be order/arrange according by their course. But eventually, there's no display at all. Not even a single letter will display.
Any help would be appreciated. Thank you so much.
In this there must some POST or GET values to get the course and cat which means
$course=$row['course'];
$cat=$row['cat'];
Since the $row is empty this is the case it will not display anything. Check with isset() like following
$course = isset($row['course']) ? $row['course'] : 'COURSE';
$cat = isset($row['cat']) ? $row['cat'] : 'CAT';
The included file include('../db.php'); please check the database connectivity has established or not?.

Query on PHP, web development

I have made a connection to mysql database and echoing values from a table.
while($data = mysql_fetch_array($res))
{
?>
<a href="nextpage.php"<?php echo $data['rowname'] ?></a>
<?php
}
?>
Problem is when I click on the particular link, on the nextpage.php, it should display only the result of the value of a href clicked. So on the nextpage.php, I have defined something like SELECT * from tablename where rowname = 'a href value'.
What's happening now is that it displays only the last rowname value regardless of whichever link I click on, very obvious!
I have tried forms, arrays, SESSIONS but to no avail. How do I fix this?
the href should be like this
<?php echo $data['rowname']; ?>
and then on next page you can use $_GET['val'] and pass it to SELECT query
Try example as below
page1.php
while($data = mysql_fetch_array($res))
{
echo "<a href='nextpage.php?id=".$data['rowname']." >". $data['rowname'] ."</a>";
}
?>
your href will look like nextpage.php?id=[your value]
nextpage.php
$qry = mysql_query("select * from [table_name] where id = ".$_GET['id']."",$con);
while($data = mysql_fetch_array($res))
{
echo $data[0];
}
on nextpage pass value using $_GET['id'] to sql query.

How to read and display one:many tables in PHP?

I am trying to combine two things which are already know how to do, but can't figure out how to combine them. Here is what I want to achieve:
I have a database with locations and events. There are several events in each location. I will be using PHP to query the database and output the code needed to display search results. I want something similar to the below:
<div id="location">
<p>Location1</p>
<div id="event">Event1</div>
<div id="event">Event2</div>
<div id="event">Event3</div>
</div>
<div id="location">
<p>Location2</p>
<div id="event">Event4</div>
<div id="event">Event5</div>
<div id="event">Event6</div>
</div>
I know that I can use select distinct to get the unique value of each location, and know that I can use a normal select statement to get all the events, however how do add all the events inside the location div?
My current PHP looks like this:
$sql ="SELECT location, event from events";
$res = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($res)){
$location = $row['location'];
$event = $row['event'];
echo "<div id="location">
<p>$location</p>
<div id="event">$event</div>
</div>";
}
My current code adds duplicates of the same location with 1 unique event in each. Even if I use select distinct I get the same results. How do I group the events have have the same location?
I think you should write something like:
$sql ="SELECT location, event from events";
$res = mysql_query($sql) or die(mysql_error());
$prevlocation = "";
while($row = mysql_fetch_assoc($res))
{
$location = $row['location'];
$event = $row['event'];
if ( $prevlocation != "" ) // Close previous div if needed
{
echo "</div>";
}
if ( $location != $prevlocation )
{
echo "<div id='location'><p>$location</p>";
$prevlocation = $location;
}
else
{
echo "<div id='event'>$event</div>";
}
}
echo "</div>"; // Close last div
If you have join of two tables, let's assume that your query looks like this:
$sql ="SELECT * FROM events
JOIN locations ON
locations.id=events.loc_id";
And then, within one loop, get events and location arrays:
$res = mysql_query($sql) or die(mysql_error());
$locations = array();
$events= array();
while($row = mysql_fetch_assoc($res))
{
$location = $row['location'];
$event = $row['event'];
$loc_id=$row['loc_id'];
$id=$row['id'];
$events[]=$loc_id.'%'.$event;
if(!in_array($id.'%'.$location,$locations)) { // avoid duplicate entries
$locations[]=$id.'%'.$location;
}
}
And, another loop (+loop inside loop):
for($i=0;$i<count($locations);$i++) {
$location=explode('%',$locations[$i]);
echo "<div class='location'>\n
<p>$location[1]</p>\n";
for($j=0;$j<count($events);$j++) {
$event=explode('%',$events[$j]);
if($event[0]==$locations[$i][0]) {
echo "<div class='event'>".$event[1]."</div>";
}
}
echo "</div>";
}
Not so elegant, but it is working, and produces valid HTML. :)
First, i wanted to make two associative arrays, and to compare keys, but i couldn't, because i couldn't convert ID keys to strings, so i made it with explode (% is separator between key and value).

Categories