PHP drop down menu help needed - php

Need help with my simple PHP menu. I'd like to have something like that: when child elements < 6 display one column if > 6 display two columns. Any advices how to make it?
Regards

Here's the basic idea, you can adapt it to work with a table:
if (count($childs) < 6)
{
foreach ($childs as $child)
{
echo htmlspecialchars($child)."<br>";
}
}
else
{
for ($n=0;$n<count($childs);$n++)
{
echo htmlspecialchars($child)." ";
if ($n%2) echo "<br>";
}
}
For a table:
if (count($childs) < 6)
{
// Single row
echo "<tr>";
foreach ($childs as $child)
{
echo "<td>".htmlspecialchars($child)."</td>";
}
echo "</tr>";
}
else
{
// Multiple row
echo "<tr>";
for ($n=0;$n<count($childs);$n++)
{
echo "<td>".htmlspecialchars($child)."</td>";
if ($n%2) echo "</tr><tr>";
}
echo "</tr>";
}
There are other ways.

If sub menu contains more than 6 links I like submenu looks like:
Link
Link
Link
Link
If more than 6 links:
Link Link
Link Link
Link Link
I'm using WP, just need advice how to make it.

Related

xPath, complicated foreach loop

I am using xPath to get a table , the table's first column has a link to another page, I want that link to link on the name column (first column). Any help would be appreciated.
Here is what I have:
echo '<table>';
foreach($xpath->query('//*[#id="CRConcernedPersonel1_DataGridCRPersonel"]/tr') as $row) {
echo '<tr>';
foreach($xpath->query('td[position() > 0]', $row) as $col) {
echo '<td>'.trim($col->textContent).'</td>';
foreach($xpath->query('a/#href', $col) as $link)
echo '<a href="'.trim($link->textContent).'"</a>'.'Link text'."\n";
}
echo '</tr>';
}
echo '</table>';
Here is the output:
http://pastebin.com/5PjCae74
Thanks!
It should work when you change
echo '<a href="'.trim($link->textContent).'"</a>'.'Link text'."\n";
to
echo 'Link text'."\n";
Currently your output reads e.g. like
<a href="CRDetails.aspx?PID=84539&Disp=1&inq=1"</a>Link text
therefore the link's not working. Changing the line as suggested should result in
Link text

I have an automated table, I can get a header on top but I also want one on the left

I am very new to PHP and html, for school I need to make a website that calculates monetary exchange rates.
Now for the calculator itself everything works. But to make it look more professional I added a table with borders etc. Then I added a table header on top but I also want to put one on the left side of my table. I just can't figure out where to put what.
This is my code so far (probably contains many mistakes. just ignore them, this is only for the table.)
<?php
if(!empty($_POST['submit']))
{
$valuta=$_POST['valuta'];
$wisselkoers=$_POST['wisselkoers'];
echo "<table border=1px>";
echo "<th>0</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th>";
for($tiental=0; $tiental<100; $tiental=$tiental+10)
{
echo "<tr>";
for($eenheden=0; $eenheden<10; $eenheden=$eenheden+1)
{
$uitkomst = ($tiental+$eenheden)* $wisselkoers;
echo"<td> ".$uitkomst."</td>";
}
echo"</tr>";
}
echo"</table>";
}
// (HTML after this)
<?php
if(!empty($_POST['submit']))
{
$valuta=$_POST['valuta'];
$wisselkoers=$_POST['wisselkoers'];
echo "<table border=1px>";
echo "<tr><th>0</th><th>1</th><th>2</th><th>3</th><th>4</th><th>5</th><th>6</th><th>7</th><th>8</th><th>9</th><th>10</th></tr>";
for($tiental=0; $tiental<100; $tiental=$tiental+10)
{
echo "<tr><th>HEADER LEFT SIDE CONTENT</th>";
for($eenheden=0; $eenheden<10; $eenheden=$eenheden+1)
{
$uitkomst = ($tiental+$eenheden)* $wisselkoers;
echo"<td> ".$uitkomst."</td>";
}
echo"</tr>";
}
echo"</table>";
}
I hope this is usefull.

Using xml attribute id in page url and on page

Im pulling in data about sports events via an xml feed, im using simplexml to do so. So far ive got a foreach loop that loops through all of the events and echos them out as a list of event names wrapped in <a> tags, pointing to a page event.php?=id (id is determined via the events attribute called id).
to do this im using
<?php
$xml = simplexml_load_file("openbet_cdn.xml");
foreach($xml->response->williamhill->class->type->market as $market) {
$market_attributes = $market->attributes();
printf("%s\n",
$market_attributes->id,
$market_attributes->name);
}
?>
the feed I'm using is http://whdn.williamhill.com/pricefeed/openbet_cdn?action=template&template=getHierarchyByMarketType&classId=5&marketSort=HH&filterBIR=N
What im having trouble with is on my page event.php i keep getting the first event in the xml feed displayed. To do this im using :
<?php
foreach ($xml->response->williamhill->class->type->market->participant as $participant) {
$participant_attributes = $participant->attributes();
echo "<tr>";
// EVENT NAME
echo "<td>";
echo "<a href=".$market_attributes['url'].">";
echo $participant_attributes['name'];//participants name
echo "</a>";
echo"</td>";
//ODDS
echo "<td>";
echo $participant_attributes['odds'];
echo "</td>";
echo "</tr>";
}
?>
I can see why it is because im not referencing the id which is in the URL of the event page. But I'm not quite sure of how to do this, any idea how I can tackle this ?
You just need to add an if within the loop so that you only target the event ID that matches the one in the querystring. A nested loop is also needed because you want to loop over each market to find the matching id, then loop over each of its participants.
foreach ($xml->response->williamhill->class->type->market as $market) {
if($market->attributes()->id == $_GET['id']) {
foreach($market->participant as $participant) {
$participant_attributes = $participant->attributes();
echo "<tr>";
// EVENT NAME
echo "<td>";
echo "<a href=".$market->attributes()->url.">";
echo $participant_attributes['name'];//participants name
echo "</a>";
echo"</td>";
//ODDS
echo "<td>";
echo $participant_attributes['odds'];
echo "</td>";
echo "</tr>";
}
break; // <-- we've found the target and echo'ed it so no need to keep looping
}
}

Wordpress single page - split images & text into columns

I admire this wordpress single (post, permalink) page, where the content is divided into two columns, the images are on the left and the text and comments are on the right. Does anyone know how I can replicate this within a wordpress theme? How would you make a post like this?
if you can edit your theme, add a magic word that you put between the columns, for example "#NEW_COLUMN#" and before you print the post in the template file, split the columns whit
$columns = explode("#NEW_COLUMN#", $content);
if(count($columns) == 1)
{
/* print a single column page */
echo $content;
}
else if(count($columns) == 2)
{
/* print a two column page */
echo "<div class='left_column'>";
echo $columns[0];
echo "</div>";
echo "<div class='right_column'>";
echo $columns[1];
echo "</div>";
}
else
{
/* ops we can't handle this many columns */
echo $content;
}
add then you need some css to make the columns end up beside each other
add_shortcode('divider', 'shortcode_divider');
function shortcode_divider(){
return '</div><div class="column">';
}
All you need to do now is add [divider] within the post;

PHP/MySQL Show first X results, hide the rest

Does anyone know how to bring in all of a mysql tables' results, only show the first X, (say 10), and then hide the rest using jquery? Basically, as I've already got the jquery, I just need to know how to show only the first X results in one div, then the rest in a seperate div.
My aim is to only show the first 10 results, but provide a link at the bottom of the page allowing the user to show all of the results. Was thinking the hyperlink could just re-execute the query but thought it would be easier to show/hide using jquery.
Many thanks in advance. S
Thought I'd add the code I'm using below
$query = "SELECT * FROM ispress WHERE active = '1' ORDER BY YEAR(date) DESC, MONTH(date) DESC LIMIT 0, 7";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet))
{
$newsArray = array();
while ($newsResult = mysql_fetch_array($resultSet))
{
$newDate = $newsResult['date'] ;
$timePeriod = date('F Y ',strtotime($newDate));
$bFirstTime = true;
if (!isset($newsArray[$timePeriod]))
{
$newsArray[$timePeriod] = array();
}
$newsArray[$timePeriod][] = $newsResult;
}
foreach ($newsArray as $timePeriod => $newsItems)
{
echo '<div class="date">' . $timePeriod . '</div>' . PHP_EOL;
echo '<ul class="press">' . PHP_EOL;
foreach ($newsItems as $item)
{
if ($bFirstTime) {
echo '<li>';
echo '<img src="'.$wwwUrl.'images/news/'.$item['image'].'" width="'.$item['imgWidth'].'" height="'.$item['imgHeight'].'" title="'.$item['title'].'" alt="'.$item['title'].'" />
<h3>'.$item["title"].'</h3>
<p>'.substr($item['descrip'],0,244).'...</p>
<p>Read more</p>
';
echo '</li>' . PHP_EOL;
$bFirstTime = false;
} else {
echo '<li>';
echo '<img src="'.$wwwUrl.'images/news/'.$item['image'].'" width="'.$item['tnWidth'].'" height="'.$item['tnHeight'].'" title="'.$item['title'].'" alt="'.$item['title'].'" />
<h3>'.$item["title"].'</h3>
<p>'.substr($item['descrip'],0,100).'...</p>
<p>Read more</p>
';
echo '<div class="clear"></div>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
}
echo '</ul>' . PHP_EOL;
}
echo '<p>Older posts...</p>'. PHP_EOL;
echo '<div id="slickbox">This is the box that will be shown and display the rest of the news results. :)</div>'. PHP_EOL;
}
else
{
echo 'We currently have no press releases available';
}
This will hide the first 10 children. How are you planning on showing the other results? Buttons, fields, jqueryui widgets?
You will just need to add a click event which calls this function.
function limit_results(start, end) {
$('#things > .thing').each(index) {
if(index < end && index >= start) {
$(this).hide();
}
}
}
limit_results(1,10);
If you have your elements in a jQuery object already (say, $('#sql-results') holds all of your results), you can always do this: $('#sql-results:lt(10)') to work with the first ten elements, and $('#sql-results:gt(9)') to work with the rest of the elements.
You have to decide yourself how efficient your approach is for this amount of data you're processing.
Right, so for your specific markup structure, you can add this to your JS:
// Obviously this is untested and probably not bug-/typo-free
(
function($) {
var $slickbox = $('#slickbox').hide();
$('<ul></ul>')
.appendTo($slickbox)
.append('ul.press li:gt(9)');
$('#slick-toggle')
.bind(
'click',
function(){
$slickbox.toggle();
}
);
}
)(jQuery);
This would involve a lot of rewriting but jquery has a datatables plugin that will display the data. To use it you need to do something like
echo '<table id="news-table">'
echo '<thead>';//Datatables needs a thead with the correct number of columns. However you don't need to fill them in.
echo '<th>Date</th>';
echo '<th>Time Period</th>'
echo '</thead><tbody>';
while ($data = my_sql_fetch_array($result)) {
echo '<td>Whatever</td>';
echo '<td>Another Field</td>';
}
echo '</tbody></table>';
The jquery is then
$('#news-table').dataTable();
I'm not sure how it would do custom no data messages and I know that with the code you have written this may not be any good to you right now but I'm posting it because it could be useful for somebody looking for pagination info or for you if you want to do something similar again. Datatables is also useful because the user can choose the number of results they want to show, what column they want to sort by and what direction to sort in.
in your query
limit 0,10
for the rest
limit 11,xxx
When you print out each row's data count each iteration by incrementing a counter. When you get to 11 start a new div that has a different id to that of your 1st div that you already defined an id for. Now using jQuery you can hide and show the 2nd div with the remaining results as you please.
Divide the return values in your php file with a character
ex:
echo "this is first value +";
echo "this is second value +";
echo "this is third value +";
use javascript to separate the return values
ex:
var ajaxArray = ajaxValues.split("+");
now all three values are placed in ajaxArray and you may use anyone you want
ex:
ajaxArray[0] = this is first value

Categories