I have problem looping out pictures from a database.
I have no problem getting the information(string) that I need from the database. The problems accure when I try to use the string as a img src-tag.
My code:
<?php
foreach ($console as $con):
echo '<li>', $con['brand'], ', ',$con['pic'], '</li>';
echo'<div class="item2">
<a href="xboxconsol.html">
<img src=',$con['pic'],'/>
</a>
</div>';
endforeach;
?>
Note, the li-elements put out the correct information but when I use it in the img-tag it adds a "/" at the end on the string.
Example of list output:
Playstation, ../playstation.jpg
The picutre however output the URL to the pic as:
../playstation.jpg/
Where do the last "/" come from and how do I get rid of it?
You need to quote the attribute value.
echo'<div class="item2">
<a href="xboxconsol.html">
<img src="',$con['pic'],'"/>
</a>
</div>';
Currently the browser is guessing what the attribute should contain.
Which comes out as:
<img src="value/">
but you want:
<img src="value"/>
You need to quote your concatenations, be very careful how you output HTML from PHP.
I'd rather concatenate the variables like this instead:
foreach ($console as $con)
{
echo "<li>{$con['brand']} {$con['pic']}</li>";
echo "<div class='item2'>
<a href='xboxconsol.html'>
<img src=\"{$con['pic']}\" />
</a>
</div>";
}
It's way cleaner and more readable.
Related
echo '<li><span class="glyphicon glyphicon-user"></span>Welcome echo $_SESSION["username"];</li>';
In the above code it is not displaying username variable.. instead it is simply echoing Welcome $_SESSION["username"];
FYI.. I have used session.start() in both the files
But how to display Welcome 'username'
You have an error. you should close quotes.
echo '<li><span class="glyphicon glyphicon-user"></span>Welcome' .$_SESSION["username"].'</li>';
If still doesn't show the username:
Turn on error_reporting or use var_dump to cehck if variable is set
<?php
error_reporting(-1);
<?php
var_dump($_SESSION);
and just note about templating. If you use this in a view (most of code is html) then try to use php temlating
<li>
<a href="signup.html">
<span class="glyphicon glyphicon-user"></span>Welcome <?= $_SESSION["username"] ?>
</a>
</li>
The part Welcome echo $_SESSION["username"]; is simply a STRING and would be echoed verbatim without any Evaluation, Processing or Parsing. And, by the way: You don't use an echo Statement within an echo statement. That should be like SAYING GOOD MORNING IN ENGLISH & GERMAN AT THE SAME TIME THROUGH THE SAME ONE SINGLE MOUTH: LOL :-)
You should have used Double Quotes to surround the String you wished to echo out and then use single quotes for the HTML DOM Element Attributes like "class" or "HREF". That way you, you could very easily echo your String without much fuss. Here's how:
<?php
echo "<li>
<a href='signup.html'>
<span class='glyphicon glyphicon-user'></span>Welcome {$_SESSION['username']}
</a>
</li>";
Try this
echo '<li><span class="glyphicon glyphicon-user"></span>Welcome '. $_SESSION["username"] .'</li>';
I have 2 tables:
TABLE users (id, name, image, user_group_id)
TABLE user_groups (id, name, menu)
column menu value contains something like this:
<img src="../images/employees/'.$row_UserDetails['Image'].'" alt="">
Now at img src its creating a problem.
'.$row_UserDetails['Image'].'
This doesn't display the value.
Why is that and how do we fix this?
I want to display results from recordset UserGroup like this:
Now the value coming from php mysql is: <img src="../images/employees/'.$row_UserDetails['Image'].'" alt="">
you see .$row_UserDetails['Image'].
This value must be abc.jpg which comes from Recordest UserDetails:
On this php page if i want to display user name, i use php echo like this: <?php echo $row_UserDetails['Name'];?>
When using echo in php you will need to escape the quotes as they cause problems.
<header id=\"header-navbar\" class=\"content-mini content-mini-full\"> <ul class=\"nav-header pull-right\"> <li> <div class=\"btn-group\"> <button class=\"btn btn-default btn-image dropdown-toggle\" data-toggle=\"dropdown\" type=\"button\"> <img src=\"../images/employees/\"" .$row_UserDetails['Image']. "\" alt=\"\"> <span class=\"caret\"></span> </button> <ul class=\"dropdown-menu dropdown-menu-right\"> <li> <a tabindex=\"-1\" href=\"my-profile.php\"> <i class=\"si si-user pull-right\"></i> My Profile </a> </li> <li> <a tabindex=\"-1\" href=\"../logout.php\"> <i class=\"si si-logout pull-right\"></i>Log out </a> </li> </ul> </div> </li> </ul>
</header>
You say you echo $row_UserDetails['Image'], but I don't see an echo statement anywhere. Also, if that PHP code is really in your database like that, I believe you are on the wrong track.
Much better would be to use some sort of placeholder, and replace that by your desired database value when rendering that menu.
Something like this:
// assume something like "... <img src="../images/employees/%{image}"> ..."
// in the menu column, where '%{image}' serves as a placeholder
echo str_replace('%{image}', $row['image'], $row['menu']);
You need to echo your tag or echo your data inside the src attribute to use the value return by $row_UserDetails['Image'].
If you have done so, this may be the problem.
You have a issue with cocatenation. Your code should be like this
<?php echo "<img src='../images/employees/".$row_UserDetails['Image']."' alt='test'/>"; ?>
This article mat help you : https://teamtreehouse.com/community/why-do-we-use-concatenation-when-outputting-this-img-tag-with-php
Best regards !
I have php file index.php
In this file to use html code I am doing:
index.php
echo '
<div>
<a class="fragment" href="">
<div>';
In href I want to put value of some php variable i.e. $url How could be done?
is this correct way?
<a class="fragment" href="<?php echo $url; ?>">
You concatenate the string by ending it and starting it again:
echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';
Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:
?>
<div>
<a class="fragment" href="<?php echo $url; ?>">link</a>
</div>
<?php
Since you are printing several lines of HTML, I would suggest using a heredoc as such:
echo <<<HTML
<div>
<a class="fragment" href="$url">
<div>
HTML;
HTML can be anything as long as you use the same tag both in the beginning and the end. The end tag must however be on its own line without any spaces or tabs. With that said, specifically HTML also has the benefit that some editors (e.g. VIM) recognise it and apply HTML syntax colouring on the text instead of merely coluring it like a regular string.
If you want to use arrays or similar you can escape the variable with {} as such:
echo <<<HTML
<div>{$someArray[1]}</div>
HTML;
if you are echoing php directly into html i like to do this
<div><?=$variable?></div>
much shorter than writing the whole thing out (php echo blah blah)
if you are writing html in php directly then there several options
$var = '<div>'.$variable.'</div>'; // concatenate the string
$var = "<div>$variable</div>"; // let php parse it for you. requires double quotes
$var = "<div>{$variable}</div>"; // separate variable with curly braces, also requires double quotes
Do it like
<?php
$url='http://www.stackoverflow.com';
echo "<div><a class='fragment' href='$url' /></div>";
If you want to maintain the echo statement you can do either
echo '<a class="fragment" href="'.$url.'"><div>';
or
echo "<a class=\"fragment\" href=\"$url\">";
The first is better for performances and IMHO is more readable as well.
If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs:
echo <<<_EOI_
<div>
<a class="fragment" href="$url">
<div>
_EOI_;
You don't have to worry about escaping quotes, constant concatenation, or that ugly dropping in and out of <?php echo $var; ?> that people do.
Insead of having to alter values for every item in the list as so
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/skateboard_l.jpg">
<img src="images/portfolio/skateboard.jpg" alt="skateboard"/>
<h3>Skateboard</h3>
</a>
</li>
Is it possible to have something like
item="Skateboard
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/[item-lowercase][if "item"_l.jpg exists, echo"_l"].jpg">
<img src="images/portfolio/[item-lowercase].jpg" alt="[item-lowercase]"/>
<h3>[item]</h3>
</a>
</li>
So I can just change the item variable for each item in the list rather than all the seperate entries. I assume this would be done using PHP or JS?
Thanks.
You want to use templates I guess.
If you want to do it client side in JS:
underscore
Mustache.js
Handlebars.js
The best solution that I can think of is to build an array (I would use PHP)... then use a while loop to build your list... put it all in a function and call it wherever you need it...
For example:
<?php
function itemList(){
$items=array ("skateboard","rollerskates","scooter","rollerblades");
reset($items);
while (list(, $value) = each($items)) {
echo '<li class="item drawing">';
echo '<a class="fancybox" rel="group" href="images/portfolio/' . $value . '_l.jpg">';
echo '<img src="images/portfolio/' . $value . '.jpg" alt="' . $value . '"/>';
echo '<h3>' . $value . '</h3>'; echo '</a>';
echo '</li>';
}
}
?>
Then in your HTML file (where you want the list to be displayed):
<ul><?php itemList(); ?></ul>
If you put the function in a separate .php file, you have to include it in the HTML document:
<?php include ('/url/of/list.php'); ?>
You can do this in PHP using echo.
For instance, if the image name was stored in $images["myImage"] and the alt was $imageAlts["myImage"
<img src="images/portfolio/<?php echo $images["myImage"]; ?>.jpg" alt="<?php echo $imageAlts["myImage"]; ?>"/>
Looks like AngularJS would help as well. It lets you do stuff like this:
<!doctype html>
<html ng-app>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.2/angular.min.js"></script>
</head>
<body>
<div>
<label>Name:</label>
<input type="text" ng-model="yourName" placeholder="Enter a name here">
<hr>
<h1>Hello {{yourName}}!</h1>
</div>
</body>
</html>
Smarty is a good template system for PHP. If you'd like to use PHP for templating over JavaScript, just start with one of the tutorials there (or look into other PHP template systems).
A classic way to do this in PHP would be to create an array of items in PHP and then iterate over the array, creating the HTML list items with the appropriate [item-lowercase] entries.
I would consider doing the [if item exists] as part of the process that builds your data array so you don't have to do anything complicated when you build your html. Then, just loop through your array and display whatever is in $theItem.
This is, of course, a simplification.
foreach($itemList as $key=>$theItem){
?>
<li class="item drawing">
<a class="fancybox" rel="group" href="images/portfolio/<?php echo $theItem ?>
<img src="images/portfolio/[item-lowercase].jpg" alt="[item-lowercase]"/>
<h3>[item]</h3>
</a>
</li>
<?php
}
<?php $picNumberSlide = "<div id='slidenumber'></div>" ;?>
Num Print
<div id="slidenumber"></div> -> (Picture slide number)
Link
<a href="share?picNum=<?php echo $picNumberSlide;?>">
Not work.
<div id='slidenumber'></div> slide number of the screen writes.
I intended to write inside the URL's
How i can do? Thank you!
(i sorry for Eng)
<?php $picNumberSlide = "<div id='slidenumber'></div>" ;?>
The above defines the $picNumberSlide variable as a div.
<a href="share?picNum=<?php echo $picNumberSlide;">
Here you are placing a div inside the href of an anchor, that will never work.
You need to assign the $picNumberSlide variable only the number you wish to use.
Then using
<a href="share?picNum=<?php echo $picNumberSlide; ?>">
will work.
$picNumberSlide should only = a number
Change <a href="share?picNum=<?php echo $picNumberSlide;">
To <a href="share?picNum=<?php echo $picNumberSlide;?>">
(You were missing the closing tag for php)