I am creating a <div> that represents a table row in sql, with php. Fine.
Then I am using a javascript function to test the values of the div (position, width etc).Fine.
But I need to pass another value to the div to be checked by the function. It is there in the database but I don't know if there is a (simple) way to do it. Ideally it would look something like this.
<div id='plumber5' class='plumber' style='width:50px;left:100px' value='numericValue'>Derek</div>
The inline styles are generated in php, and can't think of a way of passing a numeric value other than by style (width, height etc) that js can detect.
eg.
<script>
a=document.getElementById('plumber5');
if (a.style.width=>75){
execute something here}
</script>
Instead of using style as a source for testing
Its very troubling!
Thanks
EDIT - solution
function checkData($type){
a = document.getElementsByClassName($type);
for(i = 0; i < a.length; i++)
{
if (a[i].getAttribute('data-dob') >= sessionStorage.Value) {
// execute something here
}
}
}
You can use the data attribute of HTML5 to add some custom data to your HTML tags:
http://ejohn.org/blog/html-5-data-attributes/
Example:
<div data-value='10' id='plumber5' class='plumber' style='width: 50px; left: 100px;'>
Derek
</div>
You can get the value like this:
<script>
a = document.getElementById('plumber5');
if (a.getAttribute('data-value') => 75) {
// execute something here
}
</script>
You can use data- attributes this way:
<div id='plumber5' class='plumber' style='width: 50px; left: 100px'
data-value='numericValue'>Derek</div>
Note: HTML5 Data Attributes are supported only in modern browsers like IE 9+, Chrome 12+, Firefox 5+.
Notice that you have an error in the style attribute. Replace:
style='width=50px;left=100px'
With:
style='width: 50px; left: 100px'
If your audience doesn't support HTML5, you can embed a div like:
<div id="plumber5" class="plumber" style="width:50px;left:100px">
Derek
<div style="display: none">numericValue</div>
</div>
That would hide the value from view, but would allow you to access it view Javascript.
HTML5 supports data-* tag attributes, so you can use:
<div id='plumber5' class='plumber' data-value='numericValue' data-myothervalue='otherOne'>
Derek
</div>
EDIT
Since it looks messy in comments, here's how to access the example values:
var myDiv = document.getElementById('plumber5');
var myVal = myDiv.getAttribute('data-value'); // 'numericValue'
var myVal2 = myDiv.getAttribute('data-myothervalue'); // 'otherOne'
Related
I have 2 div's, if one is shown with a class="goodbye" - how do I remove the other div with php?
Or do I have to use jQuery?
<!-- this div should not show if the below class="goodbye" is on the page -->
<div class="hello">
Hello
</div>
<div class="goodbye">
goodbye
</div>
Javascript, not PHP.
if ($('.goodbye').length > 1) {
$('.hello').hide();
}
PHP, being a server-side scripting language, can't manipulate the DOM. If the condition you're using to evaluate the display of your <div>'s is processed server-side, then you could use PHP to echo one <div> or the other. Otherwise, use jQuery or JavaScript to manipulate the DOM client-side.
To answer the direct question. Remove it using PHP:
if($hello) {
echo "<div class=\"hello\">Hello</div>";
} else {
echo "<div class=\"goodbye\">goodbye</div>";
}
You can't do this with PHP since it is a server side language. Once the page is rendered you'll have to use a client side language. Yes, you can use jQuery(Javascript):
//when to handle..
$("input").click(function() {
$(".hello").toggle();
$(".goodbye").toggle();
});
http://jsfiddle.net/kfhb7gzq/
Here is a CSS option using the sibling selector. If .hello is sibling to .goodbye: display: none;
.goodbye + .hello {
display: none;
}
<div class="goodbye">
goodbye
</div>
<div class="hello">
Hello
</div>
jsFiddle with goodbye ( hides hello )
jsFiddle without goodbye ( shows hello )
This solution requires reordering the elements because the sibling selector doesn't select the previous.
You can't do it in PHP once the page is rendered... Use jQuery instead. There are lots of different ways to do this.
Use .is(":visible")); to see if the class is visible or not.
Try this example:
$('#clickme').hover(function () {
$('.hello').hide();
$('.goodbye').show();
alert($('.goodbye').is(":visible"));
}, function () {
$('.goodbye').hide();
$('.hello').show();
alert($('.goodbye').is(":visible"));
});
JSFiddle Demo
I have a responsive website and I need some PHP conditions depending on the windows width (or media queries).
Example:
if ($window_width > 1400px) {
echo 'Your window is wider than 1400px';
}
elseif ($window_width > 1000px) AND ($window_width < 1399px) {
echo 'Your window is between 1000px and 1399px';
}
else {
echo 'Your window is narrower than 1000px.';
}
Thanks for your help!
check this
Goolgle Mobile Detect
Try to use http://www.php.net/get_browser and check for isMobileDevice field. It might help only, of course, if the path to browscap.ini is set up in php.ini. If not, you can use php classes like https://github.com/garetjax/phpbrowscap
Nope you can not do it with php.
php is strictly server side
user javascript instead.
Below is my code to get device resolution using javascript
<script>
screenWidth = window.screen.width,
screenHeight = window.screen.height;
console.log(screenWidth);
console.log(screenHeight);
</script>
In views you can show/hide divs, something like this:
<style>
#web {display: block;}
#mobile {display: none;}
#media screen and (max-width: 320px) {
#web {display: none;}
#mobile {display: block;}
}
</style>
<div id="mobile">
<?php echo "is mobile"; //include("page_mobile.phtml"); ?>
</div>
<div id="web">
<?php echo "is web"; //include("page_web.phtml"); ?>
</div>
I'm assuming this is for device detection. I'm not sure if you can detect window width using PHP alone. If you can then this information would appear in the HTTP headers. I would recommend using an open source PHP class built for this: http://mobiledetect.net/
Here is the trick:
Evaluate the window width with js, then load your PHP in a frame and put the width in a parameter. Your PHP script then can read that parameter and perform different conditions depending on that value.
Here is the js part:
<script type="text/javascript">
var width = window.innerWidth;
document.write('<iframe src="content.php?w='+width+'"></iframe>');
</script>
Within your content.php file just read the width parameter and do something with it:
<?php
//Get width of browser window
$width = $_GET['w'];
echo ('width: '.$width);
?>
I need to print an html table with row heights sets dynamically based on some values from the database using PHP. seems that html 5 doesn't support inline height and with tags and using css instead.
My requirement is to generate an html file and then convert it into pdf using DOM pdf.
Please guide me how to set these parameters dynamically inline or using css or whether a library already available for the same purpose.
I Googled a lot, but unable find any results matching my requirement.
Also am attaching final output format
(In answer column i printed some values which is the height required for each row)
Thanks in advance
You can use inline styles:
<tr style="height: 300px;"></tr>
I am not sure if you can effectively set the height of a <tr> tag, so you might have to set the height of each <td> in the row individually. Give it a try.
Furthermore, I am not sure how you have your array of rows and columns structured, but this might shed some light on how to do it.
<?php
$array=array(array(50,'r1c1','r1c2'),array(50,'r2c1','r2c2'));
echo '<table>';
foreach($array as $row)
{
echo '<tr style="height: '.$row[0].'px;">';
echo '</tr>';
for($i=1;$i<count($row);++$1)
{
echo '<td>'.$row[$i].'</td>';
}
}
echo '</table>';
?>
If you still need help, post the exact array you wish to turn into a <table> and I will do my best to assist.
If I understand this right, your table rows can be different sizes from each other, but for each row there is a rule in database, that sets row's height, no matter what height the content of the row, right? Then you can use something like this:
<html>
<head>
<style>
<?php foreach($yourRows as $key => $row) { ?>
#row<?=$key;?>{
height: <?=$row['height']; ?>px;
}
<?php } ?>
</style>
</head>
<body>
<table>
<?php foreach($yourRows as $key => $row) { ?>
<tr id="row<?=$key; ?>">
...
</tr>
<?php } ?>
</table>
</body>
In the style tag you can replace "#row<?=$key;?>" with "#row<?=$key;?> td"
Updated
Anyway, if you want to use the inline styling, you can make it happen like that:
<html>
<body>
<table>
<?php foreach($yourRows as $row) { ?>
<tr style="height:<?=$row['height']; ?>px">
Or you can apply height to td instead of the tr...
</tr>
<?php } ?>
</table>
</body>
If you think that jQuery might work here is a suggestion. I'm not sure it works with DOMPDF but as we're dynamically creating CSS it should be fine once the DOM has loaded.
If you know exactly the heights of each row - then select them using jQuery using eq.
$(document).ready(function() {
$('table tr').eq(1).css({'height':'250'});
$('table tr').eq(3).css({'height':'450'});
});
Here is the fiddle.
That way you don't have to modify the output but you have to make the assumption the content isn't going to be higher than your fixed height.
If you need this to be more dynamic then you'll need to either associate identifiers to your rows, like a class or something like that. Or alternatively, if you have a pattern in your content is to create a regular expression that scans your content and identifies it that way - then you can apply CSS rules to these rows once matched using jQuery.
EDIT
OK so I may have slightly misunderstood if you have the height value stored in the database. It also looks as though you've determined already that you're unable to use inline styles.
Here is my next suggestion.
You're building the table from a loop so it probably looks something like this.
foreach($rows as $row) {
echo '<tr data-height="'.$row['height'].'"><td>...</td></tr>;
}
if you add data-height="'.$row['height'].'" then you have a value that we can get using jQuery's data like so.
$(document).ready(function() {
$('table tr').each(function() {
var height = $(this).data('height');
$(this).css({ 'height': height });
});
});
Here is an example fiddle with static data-height values. Let me know how you get on.
Okay so I have this portfolio page where I display a couple of thumbnails, and you can order it by tags, so for example like this:
year 1
And this works fine. However, my thumbnails display at three on a row, so only the first two should have a right margin, the third one no margin.
I used PHP to do this which works fine.
if ($result=$link->query($query)) {
for ($i=1; $i <= $result->num_rows; $i++) {
$row= $result->fetch_assoc();
$id = $row['number'];
$title = $row['title'];
$bgthumbnail = $row['thumbnail'];
if($i%3 == 0){
echo "
<div class=\"thumbnail\">
<a href=\"portfoliodetail.php?id=$id\">
<div class=\"thumbnailOverview noMargin\" style=\"background: url('images/portfolio/thumbnails/$bgthumbnail'); background-position: center center;\">
<div class=\"latestWorkTitle\">$title</div>
</div>
</a>
</div>
";
} else {
echo "
<div class=\"thumbnail\">
<a href=\"portfoliodetail.php?id=$id\">
<div class=\"thumbnailOverview\" style=\"background: url('images/portfolio/thumbnails/$bgthumbnail'); background-position: center center;\">
<div class=\"latestWorkTitle\">$title</div>
</div>
</a>
</div>
";
}
}
$result->close();
}
However, when I click a tag, the margin doesn't update. So when a thumbnail was given no margin in the overview because it was the third one in row, when it displays first because of a chosen tag, it also receives no margin.
Of course this is because nothing "refreshes" or something, but I was wondering if there is an "easy" way to fix this problem? To make the PHP loop run again or something?
You must to set/remove noMargin class name via javascript:
$('.year-clicker').click(function (event) {
event.preventDefault();
var year = $(event.currentTarget).data('year');
$('.thumb').hide().removeClass('noMargin').filter('.year' + year).show();
$('.thumb:visible').each(function (i, e) {
if ((i + 1) % 3 == 0) {
$(e).addClass('noMargin');
}
});
return false;
});
Try out this jsfiddle http://jsfiddle.net/xgE3K/1/
unless your "tags" are recalling the page - so that the php is re-executed - you probably want to look at javascript (or possibly ajax) to do the reformatting of the layout.
Depending on the quantity of thumbnails and the variety of tags, you might use the php to create a div (with a relevant id and a style="" attribute) for each of the different filter tags - containing the layout of the thumbnails for that tag (so you can ensure your layout is fine for each view).
i.e. repeat your code above enclosed by a unique div tag for each view.
Make the default view div visible (style="display: block") and the others hidden (style="dsplay: none").
Then have a javascript function that is executed on any tag click. This will make the relevant div visible and the rest hidden, by changing their style value as above.
Uses a bit more memory, but your switching between views will be quicker than doing a reload.
Despite all this, I think it's cleaner and more scalable to recall the page with the relative filter (depending on the tag) then you will have more control over the layout.
An ex developer of ours when working on one of our first versions of our internal PHP framework integrated the dropdown element of main navigation using javascript and I need to get a fix applied for IE8 which is causing the dropdown to appear offset even though using CSS displays fine in Firefox / Chrome.
The site in question is http://www.benchmemorials.co.uk/
In the event there is sub menu items from the main navigation that use a dropdown, javascript is called to display this I believe (below)...
<script type="text/javascript">
$(document).ready(function() {
var position = $('#link_why-buy-from-us').offset();
$('.dropdown').css(position);
$('#link_why-buy-from-us').mouseover(function() {
$('.dropdown').show();
});
$('.dropdown').mouseover(function() {
$('.dropdown').show();
});
$('.dropdownstyle').mouseover(function() {
$('.dropdown').show();
});
$('#link_why-buy-from-us').mouseleave(function() {
$('.dropdown').hide();
});
$('.dropdown').mouseleave(function() {
$('.dropdown').hide();
});
$('.dropdownstyle').mouseleave(function() {
$('.dropdown').hide();
});
});
</script>
I'm not too hot on javascript but from what I gather, I presume the above is instructing the drop down to appear below the 'Why Buy from Us' top navigation menu item. As I mention, this is working as expected in Firefox/Chrome.
However, the issue appears to be the fact that somewhere along the line, inline CSS is being generated dynamically for the dropdown class - this is dynamically generating
style="top: 61px; left: 964px; display: none;"
Every file on the server has been searched and nowhere is this specified, my only guess is that the javascript above is somehow creating this line of CSS which is therefore preventing me from altering the position of the dropdown in the IE only stylesheet to fix the display in IE8.
The rest of the code for the dropdown menu from the php file is below:-
<div class="dropdown"><div class="dropdownstyle">
<?php
mysql_select_db($database_config, $config);
$query_sub_pages = "SELECT * FROM `pages` WHERE site_id = '".$current_site_id."' AND menu_location = 'sub' ORDER BY `order` ASC";
$sub_pages = mysql_query($query_sub_pages, $config) or die(mysql_error());
$row_sub_pages = mysql_fetch_assoc($sub_pages);
$totalRows_sub_pages = mysql_num_rows($sub_pages);
$current_sub_link = 0;
do {
$current_sub_link = $current_sub_link + 1;
?>
<p<?php if ($current_sub_link != $totalRows_sub_pages) {echo ' style="margin-bottom: 10px;"';}; ?>><a class="sub_link" href="<?php echo $site_base.$row_sub_pages['page_location']; ?>"><?php echo $row_sub_pages['page_display_name']; ?></a></p><?php //if ($current_sub_link != $totalRows_sub_pages) {echo '<br />';}; ?>
<?php } while ($row_sub_pages = mysql_fetch_assoc($sub_pages)); ?>
</div>
</div>
As you can see, there is no inline CSS applied to .dropdown. All CSS from the sylesheets can be viewed if you inspect element in browser.
Please could anyone advise how or if it is possible to prevent this dynamic CSS positioning from being generated or an alternative / easier method of ensuring the dropdown appears consistently across all browsers including IE?
Thanks in advance.
The positioning is done by this code:
var position = $('#link_why-buy-from-us').offset();
$('.dropdown').css(position);
It takes the position of #link_why-buy-from-us relative to the document, and adds it to the .dropdown element.
I don't know if you're familiar with jQuery, but the code above is written using that JavaScript library. For more documentation about .offset(), look here: http://api.jquery.com/offset/