I have this little select code which should provide a 'zebra' even/odd rows. I don't understand how to change the css for that:
1, every other that will be listed (and not every second) should have .even css
2, if one of them clicked should be bold as well
(I could not figure out, how to merge these two issue)
Any help would be appreciated, from a beginner.
<div id="left">
<?php
$query = $pdo->prepare('SELECT id, name FROM test1 ORDER BY name ASC');
$query->execute();
?>
<ul>
<?php foreach ($query as $i => $row) { ?>
<li>
<?php if ($i)?>
<input name="checkbox_add[]" id="test_<?php echo $i ?>" type="checkbox" value="<? echo $row['id']; ?>"/>
<label for="test_<?php echo $i ?>"><?php echo $row['name']; ?></label>
</li>
<?php } ?>
</ul>
</div>
You should define a class odd or even (depends on which one you would like to have in alternating color) in your CSS.
Let's say you chose 'odd'. Then define a counter in your PHP code and check whether the remainder modulo 2 is equal to 1 -> if so add class 'odd' to the <li>.
<div id="left">
<?php
$query = $pdo->prepare('SELECT id, name FROM test1 ORDER BY name ASC');
$query->execute();
$idx = 0;
?>
<?php if ($idx % 2 == 0): ?>
<li>
<?php else: ?>
<li class="odd">
<?php endif; ?>
<?php
$idx++;
if ($i): ?>
...your <input> and <label>...
However, bolding the corresponding row on clicking it is something that you would preferrably do in Javascript, as it is a client-side event, the code responding to that belongs on the client side, too. Here is a crude solution, just to show what I mean, but it is not recommended with respect to clean separation of concerns and "unobtrusive" Javascript. Ideally you would put this in a separate Javascript file that attaches the event handler form within Javascript, this doesn't belong in the HTML if you want to keep it clean.
<input type="checkbox" onclick="this.parentNode.className='bold'" ...>
It would be easier to do it with jquery or prototype or something similar. I would do it with prototype, something like this:
$$('ul li:nth-child(odd)').invoke('addClassName', 'alt-row');
// Add class "alt-row" to even table rows
So, select all odd numbered li items, and apply proper css for it (invoke). You do the same thing with the odd list items, just apply other css
And for the bold part, simply add onClick event for every li item, and set style that will bold it, something like this:
<li onClick="this.className='bold'">Something</li>
Related
Ok well i have this array that consists of 3 arrays of objects so its looks like
$invoice
$invoice->foo
$invoice->foo->bars
$invoice->bars
$invoice->foobars
i have all the foo bars displayed in a table like
<?php foreach($invoice->foo->bars as $bar) { ?>
<tr>
<td>
<?php echo $bar->some_field;
</td>
<td>
<?php echo $bar->another_field;
</td>
</tr>
and i have a dropdown button at that looks like
<ul>
<li>edit</li>
<li>Delete</li>
<li>Add foobar</li>
</ul>
Would i am currently checking to see if the bar has a foobar by doing this
for ($i=0; $i < count($invoice->foobars); $i++) {
$foobar_bars[] = $invoice->foobars[$i]->foobar_bar_id;
}
and inside the $invoice->foo->bars loop i check to show the button like
<ul>
<li>edit</li>
<li>Delete</li>
<?php if(!in_array($bar->dd_id, $companion_bars)){ ?>
<li>Add Foobar</li>
<?php } ?>
</ul>
I was wondering if this is the most efficient way to do this or is their better ways to check whether the bar has a foobar
I think the most efficient way would be to actually have a method that tells you whether or not this is the case:
if ($bar->hasCompanionBar()) {
// the condition is TRUE
}
That way you can defer the decision what is most efficient to later. That will allow you to create the most efficient code over the lifetime of the codebase.
I have a list of names in a database that i want to display one by one
(also for bonus points, another column in the database is a Boolean value for if a task is completed or not. if this is true i want the css content box background to be green instead of red.)
so how can i select a name from row one, put it to a PHP variable, then select the value from the "Name" column in row 2 and put that to another PHP variable or the next item in the array?
thanks for any help!
<html>
<head>
<title>Title</title>
<link rel="stylesheet" type="text/css" href="mngPWinCSS.css"/>
</head>
<body>
<?php
$dsn ='mysql:host=****.******.com;dbname=o****_**n';
$username='********';
$password ='******';
mysql_connect('localhost',$username,$password);
$query=mysql_query("SELECT Name FROM CLOAS_Team LIMIT 0,1");
$bob="dkajfk";
$url=$_SERVER['REQUEST_URI'];
header("Refresh: 60; URL=$url");
$com[1]="i";
$com[2]="i";
$com[3]="i";
$com[4]="i";
$com[5]="i";
$com[6]="i";
$name=mysql_fetch_array($query);
?>
<div id="content">
<img src="logjpg.JPG" alt="Smiley face" height="50" width="200">
<h3>CLOAS Tracker</h3>
</div>
<div id="Content">
<?php
?>
<div id="complete">
<h3names>
<?php
echo $name['Name'];
?>
</h3names>
</div>
<div id="incomplete">
<h3names>Name2</h3names>
</div>
</div>
</body>
</html>
First you need to change your SELECT query to select all of the rows that you wish to display, perhaps by taking off the LIMIT clause. Something like this;
$result=mysql_query("SELECT Name FROM CLOAS_Team");
(This will get you all of the names in your table.)
Next, you need to loop through the results you got from this query, like so;
$names = array();
while($row = mysql_fetch_assoc($result))
{
$names[] = $row['Name'];
}
This will put them into the array $names for you, which you can then work with. Instead of putting them into the array, you might want to output them immediately, perhaps like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div>
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>
However, you have many more errors in your code. Such as;
You can't just invent html elements called <h3names>
I doubt that you want to set the id attribute to 'incomplete'. An id should be unique, I expect you should be putting this in as a class (class = "incomplete")
I don't think your line header("Refresh: 60; URL=$url"); will do anything as your headers have already been sent to the page. If you want this line to work, it needs to be right at the top, BEFORE any output has been sent to the browser.
And for the bonus point, include the 'Completed' field in your query (if that is what it is called) and use this to add a style to each <div> element that you display in your loop. So your query might become;
$result=mysql_query("SELECT Name, Completed FROM CLOAS_Team");
And your loop would now be like this;
while($row = mysql_fetch_assoc($result))
{ ?>
<div style = "background-color:<?php echo $row['Completed'] == true ? 'green' : ' red'; ?>">
<h3>
<?php
echo $row['Name'];
?>
</h3>
</div>
<?php } ?>
I have this PHP function, which I use to display my userdata. If run once it will basicly display a div with the userdata in it.
function listings($fornavn, $efternavn, $email, $adresse, $tlf, $postnr, $city, $fodselsdag, $brugerid,$bartype,$idbar)
{
?>
<div class="container">
<span class="records" id="id-<?php echo $brugerid;?>">
<div class="customer bidDiv clearfix">
<?php if ($bartype=='temp_vip') { ?>
<ul>
<li>
<span class="actionSpan" id="<?php echo $brugerid;?>" value="<?php echo $idbar;?>">
<a class="edit-opt" id="godkend" href="#">GODKEND</a>
</span>
</li>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Afvis</a>
</span>
</li>
<?php }else{ ?>
<ul>
<li>
<span class="actionSpan" id="delete-<?php echo $brugerid;?>" value="<?php echo $bartype;?>">
<a class="delete-opt" id="delete" href="#">Slet</a>
</span>
</li>
<li>
<a class="edit-opt" href="editform.php?id=<?php echo $brugerid."&bartype=".$bartype;?>" rel="facebox">Rediger</a>
</li>
<?php if ($bartype =='vip'){?>
<li>
<a class="print-opt" href="print.php?id=<?php echo $brugerid;?>" rel="facebox">Print</a>
</li>
<?php }else{
// Dont render vip link
}}?>
</ul>
<p class="contact-data">
<?php echo $email;?><br>
Tlf.: <?php echo $tlf;?>
</p>
<div class="base-data">
<h4><?php echo ucwords($fornavn)." ".ucwords($efternavn);?></h4>
<p>Fødselsdag <?php echo $fodselsdag;?></p>
<address><?php echo ucwords($adresse) ." ". $postnr ." ". ucwords($city);?></address>
</div>
</div><!-- end customer -->
</div>
</span>
<?php
I mostly use this function in a loop, to display all users from my MySQL database, who has a specific relation to something else.
This is all working great. But currently it would pull all results at once. Right now I only have 5 members, so its no problem, but say I get 800 or 2000 members, the list of users would get very long.
So I want to implement some kind of limit on how many users it displays, but still be able to browse through all the users. Many sites use something like pages and split up the results that way. Links like:
[1] [2] [3] [Last page>>]
I can't figure out how to start doing this? How would I proceed?
Also looked into this Jquery plugin:
http://andersonferminiano.com/jqueryscrollpagination/
But it keeps reloading my PHP file which results in the Loops being restarted, and it simply displays the results over and over again endlessly.
Can someone help me with the logic behind creating this? Or better, point me in a direction where I could use the jquery pluging above - where it only loads the loop one time, and renders the results as I scroll.
Any help or pointers will be greatly appreciated!
Thanks
Jquery pagination by default will only break-up the given table into several tabs on the page, that is all the data is actually loaded. There are some that let you do an AJAX request to fetch the next page, to work in conjunction with your PHP code.
Ideally, you want to limit the query using LIMIT (number of rows in the result object) and OFFSET (Start from row X ) which will give you number of records starting from your offset row up until the limit, and then use logic on the PHP side to determine the maximum number of pages.
$page = $_POST["page"] * 25; // make sure that page 0 is the starting page or calculate
$SQL = "SomeQueryText LIMIT 25 OFFSET '$page'" ;
$result = query ($SQL);
Ok,
Firstly, if you click on the questions link at the top of this page, each question has some buttons at the bottom that pertain to the question. when you mouseover them it shows more about the button. How is this done? I want to do this on my site.
So basically, i am using a php while loop to echo listitems's queried from a users id in mysql.
each listitem contains some more block and inline elements. some of those block elements have onmouseover/mouseout events attached to them. yet if i use the same class name on those elements, when i trigger a mouseover, it triggers every element with that class name. I am new to php / js / jquery, and not sure on the best way to go about it. any help would be grand. Example below.
<ul class="ulclass">
<?php
$link = mysql_query("SELECT * FROM table WHERE id='".$_SESSION['id']."' ORDER BY lid");
$i = 1;
while ($row=mysql_fetch_assoc($link)) {
$ico = $row['url'];
echo '
<li>
<a href="'.$row['url'].'" target="_blank" >
<div class="title">'.$row['title'].'</div>
</a>
<div onclick="/*here i want to change the next div.css display to block*/">
<img src="something.png" class="something_img"/>
<div class="drop_menu" id="drop_menu'.$i.'"
onmouseout="t=setTimeout(\'/*here i want to change this div.
css display back to none*/\',300);" >
<form method="post" action="" onmouseover="clearTimeout(t);">
<input type="hidden" name="deletetitle" value="'.$row['hash'].'"/>
<input type="submit" class="" name="delete" value="x"/>
</form>
</div>
</div>
</li>';
$i++;
}
?>
</ul>
let's fix some little things first. You don't really need to put all the HTML in a string, you can just do stuff like:
<?php
while ( $i < 10 ) {
?>
<li>Line number <?php echo $i; ?></li>
<?php
$i++;
}
?>
This way you will retain syntax highlighting and you won't have all kinds of problems that will arise from using string (like having to escape all single quotes etc.).
On the subject of JavaScript / jQuery – you shouldn't really use inline event handlers, such as onclick / onmouseover. It's really hard to maintain mixed up code, it's already enough there is HTML and PHP, don't add JavaScript to the same place. You should put in a separate file (or at least in a separate <script> tag before the closing </body> tag) and hook to the elements by their classes. I simplified your code a little, I am also not 100% sure what you wanted to achieve with the code you posted, but judging by the example of stackoverlow tag links, I will do something similiar:
<a href="'.$row['url'].'" target="_blank" class="tag">
<div class="title">'.$row['title'].'</div>
<div class="drop-out">Content of the drop-out.</div>
</a>
So, we have class tag for the link, and we want to hover it and see the internal element, and we take the mouse out it should disappear, let's see what jQuery we need for that (don't forget to add it to your page):
$('.tag').hover(
function () {
// `this` points to the DOM element we are hovering
$(this).children('.drop-out').css({
display : 'block'
, opacity : 1
});
}
, function () {
$(this).children('.drop-out').animate({
opacity : 0
}, 350, function () {
$(this).css('display', 'none');
});
}
);
Here's the example: http://jsfiddle.net/R6sYD/
jQuery methods used in this example:
http://api.jquery.com/hover/
http://api.jquery.com/children/
http://api.jquery.com/css/
http://api.jquery.com/animate/
Hope this helps.
I'm working on a full screen web app for the iPhone/iPod, using the latest jQTouch.
On the first "page" (as in ) a list is generated from a mysql database. Every item of that list should be a link to another "page" and pass on a variable that is unique to that item (so that the next "page" knows which item was clicked on). For now I have this:
<ul class="rounded plastic">
<?php
$data = mysql_query("SELECT * FROM tempCursist WHERE achternaam BETWEEN 'a%' AND 'e%' ORDER BY achternaam, voorletters");
while ($row = mysql_fetch_assoc($data)) { ?>
<li>
<a href="#>
<?php echo $row['achternaam'].", ".strtoupper($row['voorletters'])." ".$row['tussenvoegsel']." (".$row['voornaam'].")";?>
</a>
</li>
<?php }; ?>
</ul>
As you can see the anchor tag is just "#", but the goal is to jump to a new "page" (using the jQTouch framework, as this is intended for an iPhone/iPod) and pass on something that is unique to the clicked on dynamically generated item, so that the next "page" knows what to work with.
By the way, the table does have a primary key ($row['id']), so maybe someone knows how to make use of that...?
Many thanks in advance!
jQtouch pages can all be loaded with a single HTTP GET and the information can be passed to the jQTouch page via jquery/javascript (the page itself hasn't refreshed, only the jQTouch page has changed).
Let's assume the id of one of your pages is 'info' and you retrieve that from your sql query.
<body>
<div id="about">
<ul class="rounded plastic">
<?php
$data = mysql_query("SELECT * FROM tempCursist WHERE achternaam BETWEEN 'a%' AND 'e%' ORDER BY achternaam, voorletters");
while ($row = mysql_fetch_assoc($data)) : ?>
<li>
<a href="#<?php echo $row['id']; // this will show the info 'page' aka div based on what we assumed above ?>">
<?php echo $row['achternaam'].", ".strtoupper($row['voorletters'])." ".$row['tussenvoegsel']." (".$row['voornaam'].")";?>
</a>
</li>
<?php endwhile; ?>
</ul>
</div>
<div id="info">
<!-- This will be shown after the 'a' tag is clicked with id of info -->
</div>
</body>
Don't forget the # in the href. You can use jQuery to find whatever information you need on the jQTouch page that is shown now. The actual page hasn't refreshed.
Note: info should NOT be used as an id in your database.