I want PHP code for displaying data from database and I want to add a specific class to first <li> .
Below are the statc form of it, but I want it dynamically from my MySQL database.
<ul class="photo">
<li class="first"><img src="images/image1.jpg" alt="" /></li> //only this first line will be giving attribute (class="first")
<li><img src="images/image2.jpg" alt="" /></li>
<li><img src="images/image3.jpg" alt="" /></li>
</ul>
<ul class="text">
<li class="first">
<h2>Image 1 caption</h2>
<p>Story............</p>
<a class="next" href="">Next>></a>
</li>
<li> // this is the caption for the line that will be giving attribute (class="first")
<h2>Image 2 caption</h2>
<p>Stories 2.................</p>
<a class="next" href="">Next>></a>
</li>
<li>
<h2>Image 3 caption</h2>
<p>image 3 story................</p>
<a class="next" href="">Next>></a>
</li>
</ul>
Assuming you hava an array of data from the DB:
<?php
$first = true;
foreach ($array as $item)
{
?>
<li <?php if ($first) { echo "class='first'"; $first = false; } ?>>
<h2><?php echo $item["someheader"]; ?></h2>
</li>
<?php
}
?>
This will loop through all the rows from the db, and assign the first class only to the first row in the list.
Related
I try to put div class in the echo but it does not work really well. The coding is for to change the login button to the username button after user login. The username button will have the drop-down which is the div class. Other things are work well in this coding except for the div class drop-down. Is there another way to put the div class in the echo? Any thought??
This is before login
What I expected after Login
What I get after Login
Below is my coding:
<div class="header-right">
<?php
if (isset($_SESSION['login_user'])) {
echo '<a class="active" ><img src="image/user.png" width="22" height="22"> '.$login_session.' <img src="image/drop.png" width="20" height="20" > </a>';
echo '<div class="box dropdown">
<ul>
<a "">edit</a>
<a "">delete</a>
<a id="logout" href="logout.php">Log Out</a>
</ul>
</div>';
} else {
echo '<a class="active" href="login.php"><img src="image/lock.png" width="20" height="20"> Login</a>' ;
}
?>
</div>
I use another way to write down this. Let me know if you have any question. I fixed some attribute missing into your anchors <a> and list some <li> elements into your <ul> element as well.
<div class="header-right">
<?php if (isset($_SESSION['login_user'])) { ?>
<a class="active" >
<img src="image/user.png" width="22" height="22">
<?= $login_session ?>
<img src="image/drop.png" width="20" height="20" >
</a>
<div class="box dropdown">
<ul>
<li>edit</li>
<li>delete</li>
<li><a id="logout" href="logout.php">Log Out</a></li>
</ul>
</div>
<?php } else { ?>
<a class="active" href="login.php"><img src="image/lock.png" width="20" height="20"> Login</a>
<?php } ?>
</div>
You may put your code in the way like below and I found out your <ul> is missing <li>.
<div class="header-right">
<?php if(isset($_SESSION['login_user'])): ?>
<!-- Login User (HTML) -->
<?php else: ?>
<!-- Not Login User (HTML) -->
<?php endif; ?>
</div>
<?php if (isset($_SESSION['login_user'])) { ?>
<a class="active" >
<img src="image/user.png" width="22" height="22">
<?php $login_session ?>
<img src="image/drop.png" width="20" height="20" >
</a>
<div class="box dropdown">
<ul>
<li>edit</li>
<li>delete</li>
<li><a id="logout" href="logout.php">Log Out</a></li>
</ul>
</div>
<?php } else { ?>
<a class="active" href="login.php"><img src="image/lock.png" width="20" height="20"> Login</a>
<?php } ?>
</div>
I have some navbar with menu and submenu, I want to appear some <div> in that menu to appear the submenu just if this menu has submenu items
The div that I want to appear are div1, div2, div3, see comments and in this condition : <?php if ($men["titre"] == $sub["parentmenu"]) : ?>
I can't put them in this foreach : <?php foreach($submenu as $sub) :?> because I need that three div just ones for all the submenu <li>
Exemple of one menu who have submenus:
<li class="Menu1">
<a class="header-menu " href="#">Menu1 title</a> //Menu1
<div class="mobile-header-nav-submenu-container"> //div 1
<ul class="mobile-header-nav-submenu"> //div 2
<div class="mobile-header-nav-submenu-li-container"> //div3
<li class="submenu1">
<a class="" href="#">Submenu1</a>
</li>
<li class="submenu2">
<a class="" href="#">Submenu2</a>
</li>
<li class="submenu3">
<a class="" href="#">Submenu3</a>
</li>
</div>
</ul>
</div>
</li>
My code:
<div class="menu>
<?php foreach ($menu as $men): ?>
<li class="<?php echo $men["titre"]?>">
<a class="header-menu <?php echo $men["lien"]?>" href="<?php if($men["lien"]){echo $baseUrl.$men["lien"];}else{echo '#';}?>"><?php echo $men["titre"]?></a>
<div class="mobile-header-nav-submenu-container"> //div1
<ul class="mobile-header-nav-submenu"> //div2
<div class="mobile-header-nav-submenu-li-container"> //div3
<?php foreach($submenu as $sub) :?>
<?php if ($men["titre"] == $sub["parentmenu"]) : ?>
<li id="<?php echo $sub["titre"]?>" class="mobile-header-nav-submenu-li">
<a class="" href="<?php if($sub["lien"]){echo $baseUrl.$sub["lien"];}else{echo '#';}?>">
<?php echo $sub["titre"]?>
</a>
</li>
<?php endif; ?>
<?php endforeach; ?>
</div>
</ul>
</div>
</li>
<?php endforeach; ?>
</div>
I'm using php to add active link state to my navigation menu which is shared dynamically across all my website pages. But when I go to other links from homepage it works fine but when I go back to homepage its give me "object not found" error.
<!--start vertical menu-->
<?php
$index="menu-items";
$top_albums_default="menu-items";
$Topartists="menu-items";
$Toplists="menu-items";
$Charts="menu-items";
$menuLinkid=basename($_SERVER['PHP_SELF'],".php");
if($menuLinkid=="index"){
$index='active';
}else if ($menuLinkid=="top_albums_default"){
$top_albums_default='active';
}else if ($menuLinkid=="Topartists"){
$Topartists='active';
}else if ($menuLinkid=="Toplists"){
$Toplists='active';
}else if ($menuLinkid=="Charts"){
$Charts='active';
}
?>
<div id="vertical-menu">
<!--<h2 class="logo">LOGO</h2>-->
<img class="logo" src="../Graphics/icons/logo.png" alt="LOGO"/>
<h6 class="logo-desc">Arcade Music Repository</h6>
<ul class="menu-items">
<li>
<a class="<?php echo $index; ?>" href="index.php" ><i class="arcd-archive" ></i></br>Browse</a>
</li>
<li>
<a class="<?php echo $top_albums_default; ?>" href="Top-albums/top_albums_default.php"><i class="arcd-music97"></i></br>Top albums</a>
</li>
<li>
<i class="arcd-microphone52"></i></br>Top artists
</li>
<li>
<i class="arcd-numbered8"></i></br>Top lists
</li>
<li>
<i class="arcd-rising9"></i></br>Charts
</li>
</ul>
</div>
<script type="text/javascript" src="../js/jquery-1.11.3.min.js"> </script>
Please check this in video
Here is my site folders structure
You are missing a forward slash on your homepage link.
Make it like this:
<ul class="menu-items">
<li>
<a class="<?php echo $index; ?>" href="/index.php" ><i class="arcd-archive" ></i></br>Browse</a>
</li>
I wanna make dynamic slideshow with css and HTML. Image will be took by id from tb_image.
id int
image_name varchar (100)
image_path (100)
How to make dynamic slideshow using php, mysql, css and html?
or Can get image by id?
include "connnection.php";
$result = mysql_query("select * from slider");
?>
<div class="accordian">
<?php
$i=1;
while($row = mysql_fetch_array($result)){
?>
<ul>
<li>
<div class="image_title">
</div>
<a href="#">
<img src="../img/<?php echo $i;?>.jpg" alt="" width="640" border="320"/> //I tried use this codes but it's error
</a>
</li>
<li>
<div class="image_title">
Slide 2
</div>
<a href="#">
<img src="img/<?php echo $_GET['id']=2;?>.jpg" alt="" width="640" border="320"/>
</a>
</li>
<li>
<div class="image_title">
Slide 3
</div>
<a href="#">
<img src="img/3.jpg"/>
</a>
</li>
<li>
<div class="image_title">
Slide 4
</div>
<a href="#">
<img src="img/4.jpg"/>
</a>
</li>
<li>
<div class="image_title">
Slide 5
</div>
<a href="#">
<img src="img/5.jpg"/>
</a>
</li>
</ul>
<?php } ?>
Not knowing exactly what you get back from your query, here's a guess:
Replace any of your <img>s with
<img src="../img/<?php echo $row['id']?>.jpg" alt="" width="640" border="320"/>
To "debug" your data, you can replace your while loop with
while($row = mysql_fetch_array($result)){
print_r($row);
}
This will print out any data found in each row.
You might also want to replace the deprecated mysql_* functions with mysqli or PDO.
Hi I am using this code to pass a data-testid parameter
--- php loop here
echo <<<_END
<pre>
<ul data-role="listview"> <li>
<a id="test" href="#test" data-rel="dialog" data-testid="$row[1]">$row[3] $row[1]</a>
</li> </ul>
</pre>
_END;
And I am using this to retrieve it on the links internal page #test:
var b = $('a#test').data('testid');
document.write(b);
The problem is that although >$row[3] $row[1]< loops correctly and changes the data-testid="$row[1]" stays the same when retrieved - even when the test number is not even in the database return anymore! It has stayed the same since the first one I called (which was the correct number). How do i get it to refresh?
I have tried no quotes and single quotes around $row[1] but the result is the same.
You may want to include some more code, as in how do you populate the rows array and where do you call the jquery function.
I've just tested this code:
<?php
$rows = Array();
for($i = 0; $i < 5; $i++){
$rows[] = Array("row0," . $i, "row1," . $i, "row2," . $i, "row3," . $i);
}
foreach($rows as $row){
echo <<<_END
<pre>
<ul data-role="listview"> <li>
<a id="test" href="#test" data-rel="dialog" data-testid="$row[1]">$row[3] $row[1]</a>
</li> </ul>
</pre>
_END;
}
?>
And it works like it should, it returns this as output:
<pre>
<ul data-role="listview"> <li>
<a id="test" href="#test" data-rel="dialog" data-testid="row1,0">row3,0 row1,0</a>
</li> </ul>
</pre><pre>
<ul data-role="listview"> <li>
<a id="test" href="#test" data-rel="dialog" data-testid="row1,1">row3,1 row1,1</a>
</li> </ul>
</pre><pre>
<ul data-role="listview"> <li>
<a id="test" href="#test" data-rel="dialog" data-testid="row1,2">row3,2 row1,2</a>
</li> </ul>
</pre><pre>
<ul data-role="listview"> <li>
<a id="test" href="#test" data-rel="dialog" data-testid="row1,3">row3,3 row1,3</a>
</li> </ul>
</pre><pre>
<ul data-role="listview"> <li>
<a id="test" href="#test" data-rel="dialog" data-testid="row1,4">row3,4 row1,4</a>
</li> </ul>
So the problem is either in populating the array, or more likely in querying it in jquery. I noticed that you're using the same id 'test' for the link, this is a bad practice. Change the link-id for each link and try again printing the data.