php array and jquery add class - php

I am trying to get desks highlighted that are available based off of a form that asks for the the day, time start and time end. I am able to echo out all the desks that are available, but I cant get the jquery to work with it.
foreach ($allData as $desk => $id){
foreach ($id as $computer){?>
<div id="<?php echo $desk?>"></div><?php
}
}
<style>
.availableDesk{
background: #000000
}
</style>
<script>
$(document).ready(function() {
jQuery( " <?php echo $desk ?> " ), addClass('availableDesk') ;
})
</script>
DESKS:
<ul class="tabs">
<li id="1A"><div id="ddesk"></div></li>
<li id="1B"><div id="ddesk"></div></li>
<li id="1C"><div id="ddesk"></div></li>
</ul>

You're missing the ?> at the end of your PHP block.
<?php
foreach ($allData as $desk => $id){
foreach ($id as $computer){?>
<div id="<?php echo $desk?>"></div><?php
}
} // You need a "?>" here
?>
There are a few things wrong with your jQuery code. First off, if $desk is an ID, you need to do $('#ID'). Second, you have a comma before addClass instead of a period.
jQuery("#<?php echo $desk ?>").addClass('availableDesk');
P.S. HTML ID's aren't supposed to start with a number. Also, you cannot have multiple elements with the same ID, I suggest you use classes instead.

I can see a couple of potential issues here...
jQuery( "<?php echo $desk ?>" ), addClass('availableDesk') ;
Syntactically, I think this should be:
jQuery("#<?php echo $desk ?>").addClass('availableDesk');
Note the # in the selector, this tells jQuery that you are looking for an id. The addClass method is available on the returned items, so you need a stop (.) not a comma (,)
The other gotcha is that you are writing the id in a foreach loop in PHP - I presume that each desk has a unique id - when you write jQuery("#<?php echo $desk ?>") the statement is outside of the foreach loop, so it won't match the ids you are targeting.
If you know that the desk is available in PHP, the best option would be to set the class as you write the desk...
<div id="<?php echo $desk?>" class="availableDesk"></div>

Related

How to add an item containing sub-items in each iteration of a loop?

Sorry if this seems very simple I'm rather new to php.
I'm drawing data from the database using a for each loop and want to add a list item for each result to a side bar while adding a new container div to the body.
What is the best way to code this?
Any help is much appreciated.
Thanks A
Edit...
My code so far...
<?php
$iidcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_iid_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
$gadcoversheets = $wpdb->get_results(
"
SELECT *
FROM $wpdb->trm_gad_cover_sheet
WHERE Return_ID_FK = '$trmrtnid->Return_ID_PK'
"
);
?>
<ul>
<?php
foreach ( $iidcoversheets as $iidcoversheet )
{
?>
<li><?php echo $iidcoversheet->business name; ?></li>
<?php
}
?>
<?php
foreach ( $gadcoversheets as $gadcoversheet )
{
?>
<li><?php echo $gadcoversheet->business name; ?></li>
<?php
}
?>
</ul>
I understand how to add the <li> items but I'm strugging on how to add a div containing some html to another div not in the <ul> tag as it seems to me this will interrupt the code for the <ul>.
Any help would be really appreciated.
Thanks A
<div>
<ol>
<?php
foreach ($uls as $ul) {
?>
<li>
<?php echo $ul; ?>
</li>
<?php
}
?>
</ol>
</div>
Ok I came to the realisation that I could simply do another exact same loop later on in the page in the div I wanted but with different output. I was trying to do it all up front save the results and output them somewhere else, unnecessary.
Clearly I was having a daft moment earlier.
Thanks for all the input.
Regards
A

PHP MySQL Order list

Can anyone help?
My code is like this:
<ol><li>{$student_Value}</li> <ol>'
it gives result:
1. Student101Name
1. Student102Name
1. Student103Name
I want something like:
Student101Name
Student102Name
Student103Name
Please help... Thank you!
Do not close the OL tag every time
OL = Ordered List
LI = List item
If you close and reopen the OL, it creates a new ordered list, and so restarting at 1.
<pre>
echo "<ol>";
foreach loop {
echo "<li>{$student_Value}</li>";
}
echo "</ol>";
</pre>
Good luck.
If you use MySQL to get the data you can add ORDER BY to the query to order the result!
Here more info how to use ORDER BY
And change the HTML Code to be:
<ol>
<li>$student_Value</li>
<li>$student_Value</li>
<li>$student_Value</li>
</ol>
PHP Code will be:
echo "<ol>";
echo " <li> $student_Value </li> ";//write all student values like this
//more student values
echo "</ol>";
It looks like your problem is more CSS related than PHP or SQL.
The reason is probably that your output is:
<ol><li>$student_Value</li></ol>
<ol><li>$student_Value</li></ol>
<ol><li>$student_Value</li></ol>
Each student should be a list item within the ordered list:
<?php
print "<ol>";
while ($row = $result->fetch_assoc())
{
print "<li>{$row['student']}</li>";
}
print "</ol>";
?>
You need something like that
<ol>
<?php
for($i=0; $i<count($studentValue); $i++)
{
?>
<li><?php echo $studentValue[$i]; ?></li>
<?php
}
?>
<ol>
I am giving you above example to tell you that under a 'ol' tag 'li' must be multiple to get your require result.

Add a unique ID to Drupal Views list groupings

I'm using Views 3 in Drupal 7 to output a list of fields and using a grouping field to generate separate lists. I need each of the groupings to have a unique ID attribute applied to the < ul > but they don't by default.
As far as I'm aware I would need to edit the views-view-list.tpl.php template but I don't know how to achieve a unique ID per iteration.
Can anyone help please?
easiest way I can think of off the top of my head...
<?php print $wrapper_prefix; ?>
<?php if (!empty($title)) : ?>
<h3><?php print $title; ?></h3>
<?php endif; ?>
<ul id="<?php echo uniqid(); ?>">
<?php foreach ($rows as $id => $row): ?>
<li class="<?php print $classes_array[$id]; ?>"><?php print $row; ?></li>
<?php endforeach; ?>
</ul>
<?php print $wrapper_suffix; ?>
that would go in your views-view-list.tpl.php file.
For future reference:
Put a div around everyting in view-views-list.tpl.php. You can (ab-)use the $title to generate unique (but consistent) id's.
Do it like this:
<?php $id = str_replace('FOR ALL UNWANTED CHARS','',$title); ?>
<div id="<?php print strtolower($id); ?>">
You can use the $view->dom_id variable. It is a unique id for that views instance.
In your .tpl.php file:
<?php print $view->dom_id; ?>
From comments in modules\views\theme\theme.inc:
<?php
// It is true that the DIV wrapper has classes denoting the name of the view
// and its display ID, but this is not enough to unequivocally match a view
// with its HTML, because one view may appear several times on the page. So
// we set up a hash with the current time, $dom_id, to issue a "unique" identifier for
// each view. This identifier is written to both Drupal.settings and the DIV
// wrapper.
?>

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.

Passing php variable to jquery

for($v=0;$v<11;$v++) {
echo "<div class='unsubscribed'><a class='button'>Unsubscribe</a></div>";
echo "<div id='$v'></div>";
}
I want onclick of the class unsubscribed to remove the div below in the same iteration. So for this i have to pass $v to jquery.
This is what i started with jquery but i don't know how to get the variable $v. How do i accomplish this?
$.ready(
function() {
$('.unsubscribed').remove();
}
);
you do not need to pass anything to jquery :
$(document).ready(function(){
$('.unsubscribed').one('click',function(){
$(this).next().remove();
});
});
This works for your current html.
To be more safe, you should add a class to the elements you want to be removed:
for($v=0;$v<11;$v++) {
echo "<div class='unsubscribed'><a class='button'>Unsubscribe</a></div>";
echo "<div class='to_be_removed'></div>";
}
This way you can reference the div you want to remove withouth it being necessarily after the unsubscribed div :
$(document).ready(function(){
$('.unsubscribed').one('click',function(){
$(this).next('.to_be_removed').remove();
});
});
A better solution might be:
<?php for($v=0;$v<11;$v++) { ?>
<div class="unsubscribed" rel="<?php echo $v; ?>">
<a class='button'>Unsubscribe</a>
</div>
<div id="<?php echo $v; ?>"></div>
<?php } ?>
$(document).ready(function(){
$(".unsubscribed").click(function(){
var div_to_remove = $(this).attr("rel");
$('#'+div_to_remove).remove();
});
});
I prefer doing it this way, because working with .next can sometimes cause problems, when you add something in between. It can be very hard to find the problem then.
This way, you simply embed the needed information about the div you want to remove into an attribute of the div that triggers the event.
Note: in this example, the function is called on clicking the .unsubscribed div - not the .button.
You also have to make sure, the removable divs have different and unique ids. If $v isn't unique, you can do e.g. something like this:
...
<div id="<?php echo $v . $i; ?>"></div>
...

Categories