Alternate text of div - php

Can i put class name in title of div? It's possible?
<div class="flag brasileña" title="phpcode"></div>
I want put in title, text of second class "brasileña" with php code or similar

<?php
$className = "flag brasileña";
$alt = explode(" ", $className);
?>
<div class='<?php echo $className ?>' title='<?php echo $alt[1] ?>'></div>
Assuming class name always has two words and alt always take the second word and since you mentioned you want to do it by php the class name is generated dynamically.
This is how you can do it but this wont serve any purpose.
Whats that you want to do with adding second word to "alt".

Related

Turning a <div> content into a $variable in php

I have this website were you can order products.
The title of the products you can order are in HTML:
<div class="whatever"> Title </div>
I want to retrieve this "title" and set my php variable $product to the value "Title".
I have search a lot on the internet but somehow I am not able to find my answer.
How can I do it?
You can use \DOMDocument->loadHTML();
Ex:
<?php
$doc = new \DomDocument();
$doc->loadHTML('<div class="whatever"> Title </div>');
View examples here:
http://docs.php.net/manual/en/domdocument.loadhtml.php
This is assuming that your source is available to php. It would probably be more pragmatic to extract the value with javascript in the client and send it with the page request. If your app is well structured, the logic that renders the title into the page in the first place is probably where you should be looking to retrieve the information rather than trying to parse the html separately.
If you mean that you would like to do this from the client side, you should be using AJAX to achieve this. However, I think you mean that you want to put the HTML in a variable. That is very simple:
$variable = "<div class=\"whatever\"> Title </div>";
And to output the HTML:
echo $variable;
You can also add multiple elements to a single variable by concatting.
$variable = "";
$variable .= "<div class=\"whatever\"> Title </div>";
$variable .= "<div class=\"whatever\"> Another Title </div>";
echo $variable;
If you mean that you want to echo a variable within a dv, that works exactly the same way:
<div class="title"><?php echo $product; ?></div>
Or better looking:
<div class="title"><?= $product; ?></div>

Highlight if link is selected

I'm trying to highlight the currently selected link in a link-bar, whereas the other links that are not selected should stay as they were. My idea is to assign a class within an if-statement (e.g. if the link contains the string "date=3", it will be assigned to class "selected) in order to style it then in CSS. But I dunno how it's done, can you help me please?
My hyperlink within PHP looks this way:
<?php echo date("l, j F",strtotime($mayday)+60*60*24*3); ?>
In your example, the link will always include date=3 !
But you want to evaluate if $_SERVER['REQUEST_URI'] contains date=3 ?
<a'.(preg_match("|date=3|",$_SERVER['REQUEST_URI']) ? "class=\"selected\"" : "").' href="<?php echo preg_replace("/&date=()/", "", $_SERVER['REQUEST_URI'])."&date=3";?>"><?php echo date("l, j F",strtotime($mayday)+60*60*24*3); ?></a>

CSS Class on end MySQL row

I am having trouble creating a solution that will target the end row of a MySQL query. Currently I have a foreach function that works through the query and displays each one as a div with the information inside:
<?php $residents = Resident::find_all();
foreach($residents as $resident): ?>
<div class="submenu">
<p class="menuitem submenuheader"><?php echo $resident->name; ?></p>
<img src="images/<?php echo $resident->image_path(); ?>" width="250" class="image" />
<p><?php echo $resident->info; ?></p>
</div>
.submenu currently has a bottom border. I need to remove this on the last row returned. I have looked at DESC LIMIT 1, however this requires another MySQL query and could make things very messy...
Addd this to your CSS:
.submenu:last-child { border-bottom: 0; }
Note: this is not supported by IE < 9.
You could switch to putting the border on the top of the element, and use the :first-child pseudo selector in CSS to remove it.
http://reference.sitepoint.com/css/pseudoclass-firstchild
The :last-child selector would be nice, but it's not supported in IE before version 9, so it's not a good idea to use it if you want compatibility.
If you separate your HTML and PHP a little this is easily achieved:
<?php
function echoBlock($resident,$pClass="menuitem submenuheader") {
echo "<div class=\"submenu\">\n<p class=\"$pClass\">\n";
echo $resident->name;
echo "</p>\n<img src=\"images/";
echo $resident->image_path();
echo "\" width=\"250\" class=\"image\" />\n<p>";
echo $resident->info;
echo "</p>\n</div>\n\n";
}
$residents = Resident::find_all();
$last=count($residents)-1;//2 element array last pos is 1
for ($i=0;$i<$last;$i++) {
echoBlock($residents[$i]);
}
echoBlock($residents[$last],"menuitem");
?>
echoBlock (which could easily be a method on a class) requires the calling code to know about the classes it uses, which isn't really separating intent but it does prevent the need for an if branch on every loop. That being said it would be less efficient but perhaps more usable to set it up as:
function echoBlock($resident,$isLast=false) {
$pClass="menuitem".($isLast?"":" submenuheader");
//...
Which then doesn't need the caller to know anything about what echoBlock does.
You could try and pop the array using array_pop(), to get the last value out of the array and then inputing it using the special class after the foreach loop.
What about
Instead of echo'ing each line one by one, create one big string in PHP, search for the last entry of "submenu" and change the class.

How do I get this while loop to dynamically produce this html?

I am building a picture gallery, that uses this code to display each product I have:
<div class="feature">
<imagetag alt="Image Caption"srcs="">
<div>
<p>
This is some information that can go along with an image.
Anything can be placed here, including images.
</p>
</div>
</div>
I need to create a while loop, that takes all the products in my database, and creates a div of the "feature" class for every instance. I have problems know exactly which symbols need to be escaped and etc. Your help is greatly appreciated.
here is my start:
<?php
($product_set = mysql_fetch_assoc($query)) {
print("<div class="feature"> <imagetage alt="Image Caption" srcs=$product_set[products_image]>"
);}
?>
If you are in a string, every doublequote should be escaped. Because it will close your string.
<?php
($product_set = mysql_fetch_assoc($query))
{
print "<div class=\"feature\"><img alt=\"Image Caption\" src=" . $product_set['products_image'] . ">";
}
?>
Fun thing is, I got a link from someone on stackOverflow about PHP templating. Which was using Smarty. So you don't have to use these print states anymore.
Have you tried:
print(htmlentities($my_html_string))
or htmlspecialchars? htmlentities converts all characters that have one to their HTML escape sequence, while htmlspecialchars converts only those that have meaning in HTML.

Set two classes from array

I want to change the classes on every third output from a query:
<?php
$style_classes = array('box','box','box no_right_margin');
$style_index = 0;
?>
I set this on the div:
<div <?php $k = $style_index%4; echo "class=$style_classes[$k]"; $style_index++; ?>>
On the third div I want the class to look like this:
<div class="box no_right_margin">
Right now it looks like:
<div class="box" no_right_margin>
You need to enclose the class names in quotes. Your script is actually outputting class=box no_right_margin. (I think the example you gave as the current output is not what the script is sending, but the view of the DOM from something like Firebug, which is showing the browser as only seeing the first class in the list)
So you could do this:
<div class="<?php $k = $style_index%4; echo $style_classes[$k]; $style_index++; ?>">
or even
<div class="<?php echo $style_classes[$style_index++ % 4]; ?>">
You should use %3 instead of %4, so you actually get indexes 0, 1 and 2.
And you need correct quotes in your HTML output:
<?php echo '<div class="' . $style_classes[$k++ % 3] . '">'; ?>
Else your browser (Safari?) would probably correct it with a " at the wrong place, as shown in your example. Btw, it's better style to use hyphens for CSS class names, not underscores (unlike IDs).

Categories