I have a sitemap that I'm trying to group by URL and stack them based on URL. Here is the sitemap https://www.sitecentre.com.au/sitemap
Basically, I need this to be dynamic. I can make this easily by doing a case, or if statements, like I've done on the Blog section, but I need it dynamic. It needs to look like this:
<li>Web Design
<ul>
<li>Nested in Web Design</li>
<li>Nested in Web Design</li>
</ul>
</li>
All based on the URL. the URL is /design then the nested would be design/nested
Then the next group would be like branding and nested on branding so it's all organised and laid out perfectly, then alphabetically ordered based on something else.
Here is our current PHP:
<?php
$pages = json_decode(file_get_contents('data/pages.json'));
foreach ($pages as $data){
foreach ($data as $key => $values){
if ($values->status === '1' && $values->visible === '1' && $values->slug !== 'home'){
if ($values->slug !== 'blog'){
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '</li>';
} else {
// Get the latest blogs
$blogs = json_decode(file_get_contents('data/blogs.json'));
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '';
echo '<ul>';
foreach ($blogs as $data){
foreach ($data as $key => $values){
if ($values->status === '1' && $values->visible === '1'){
echo '<li>' . $values->heading . ' - Last Updated: ' . date('d/m/Y', strtotime($values->date_modified)) . '</li>';
}
}
}
echo '</li></ul>';
}
}
}
}
?>
It clearly needs a bit of work in order to make this work, but I cannot for the life of me work out where to start.
Related
I have very little experience with PHP, but I'm taking a class that has PHP review exercises. One of them is to create a function that uses a loop to return all values of an array except the first value in an unordered list. I'm assuming there's a way to do this using a foreach loop but cannot figure out how. This is what I had but I feel like I am far off:
<?php
$array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');
$myName = $array['myName'];
$favColor = $array['favColor'];
$favMovie = $array['favMovie'];
$favBook = $array['favBook'];
$favWeb = $array['favWeb'];
echo '<h1>' . $myName . '</h1>';
function my_function() {
foreach($array == $myName){
echo '<ul>'
. '<li>' . $favColor . '</li>'
. '<li>' . $favMovie . '</li>'
. '<li>' . $favBook . '</li>'
. '<li>' . $favWeb . '</li>'
. '</ul>';
}
}
my_function();
?>
The correct syntax of foreach is
foreach (array_expression as $key => $value)
instead of
foreach($array == $myName){
function that uses a loop to return all values of an array except the
first value
I'm not sure, what exactly you mean by except the first value. If you are trying to remove first element from the array. Then you could have used array_shift
If you are supposed to use loop then
$count = 0;
foreach ($array as $key => $value)
{
if ($count!=0)
{
// your code
}
$count++;
}
Change the code to following
<?php
$array = array('myName' => 'Becca', 'favColor' => 'violet', 'favMovie' => 'Empire Strikes Back', 'favBook' => 'Lullaby', 'favWeb' => 'twitter.com');
$myName = $array['myName'];
echo '<h1>' . $myName . '</h1>';
function my_function($array)
{
$count = 0;
echo "<ul>";
foreach($array as $key => $value)
{
if($key != "myName")
{
echo "<li>".$value."</li>";
}
}
echo "</ul>";
}
my_function($array);
I've got a list of "sites" in an SQL database that I'd like to display on a web page. I need to create a div ID for every site. For the time being I'm doing this (fetch the table into a $_SESSION array, get and display the name of the sites in a loop and do an echo):
<div id='leftnavigation1' class="leftnavigation">
<ul>
<?php
$maxKeys = max(array_keys($_SESSION['usersmeter'])); //check how many sites are in the usersmeter table
for ($i = 0; $i <= $maxKeys; $i++) { //loop every key in the array
$siteName = $_SESSION['usersmeter'][$i]['siteName'];
echo "<li id='siteID$i'><a href='#'><span>" . $siteName . "</span></a></li>"; //display the name of the sites
}
?>
</ul>
</div>
It works well but I think it is a bad practice to include php code into the View.
How else can I do? An AJAX in Javascript ?
Thanks,
Like that you will br try:
<?php
$my_array = array(
'GSU4300' => 'Lili Markina',
'GSU4301' => 'John Kokina',
'GSU4304' => 'Bill Clinong',
'GSU4305' => 'Obamark Chiko'
);
echo '<ul id="somethig">';
foreach ($my_array as $k => $v) {
echo '<li id="' . $k . '">' . $v . '</li>';
}
echo '</ul>';
?>
I am trying to display items from an XML feed on a page
This is my code...
$feed = 'http://awebsite.co.uk/directory/rssfeed.php?thecondition=(title%20LIKE%20'%british%'%20OR%20description%20LIKE%20'%british%')';
$xml = simplexml_load_file("$feed");
if ( $xml->channel->item !='' ) {
foreach ($xml->channel->item as $item) {
echo '<li>' . $item->title . '</li>';
}
}
else { echo 'None'; }
However, due to the complicated feed URL it is giving lots of errors.
Note: I have changed the domain in the URL for this example.
How can I make this work?
Can't judge anything without seeing the XML source first but if it's a standard RSS channel file you should be iterating through your items something like this:
$items = $xml->xpath('/channel/item');
foreach ($items as $item) {
echo '<li>' . $item->title . '</li>';
}
Also the initial condition: if ( $xml->channel->item !='' ) {/*...*/} isn't really going to work.
We have a client who wants to output cinema listings on their website. This particular cinema has provided us with a link to retrieve the information from. This link is simply outputted in plain text, with the elements seperated by ~|~
I am using explode and strpos to divide the listings into divs however it still isn't good enough for what I am wanting to achieve. I just can't get my head around what I can do to format this correctly. This is my code:
<?php
$theArray = explode("~|~", file_get_contents("THE URL"));
echo "<ul id=\"cinemaListings\">";
foreach($theArray as $item){
$findcinemaid = '~77';
$findcinemaname = 'Cinema Name';
$findx = 'X';
$findu = 'U';
$findgen = 'Gen';
$findlink = '.ie';
if(strpos($item, $findcinemaname) !== false){
echo "<div class='cinemaName'>" . $item . "</div>";
}elseif(strpos($item, $findgen) !== false){
echo "<div class='cinemaGen'>" . $item . "</div>";
}elseif(strpos($item, $findcinemaid) !== false){
echo "<li>";
}elseif($item == $findx){
echo "<div class='cinemaX'>" . $item . "</div>";
}elseif(strlen($item) == 8){
echo "<div class='cinemaCode'>" . $item . "</div>";
}elseif(strlen($item) == 3){
echo "<div class='cinemaListId'>" . $item . "</div>";
}elseif(strlen($item) == 2){
echo "<div class='cinemaListId'>" . $item . "</div>";
}elseif(strlen($item) == 1){
echo "<div class='cinemaListId'>" . $item . "</div>";
}elseif(DateTime::createFromFormat('H:i', $item) !== FALSE){;
echo "<div class='cinemaTime'>" . $item . "</div>";
}elseif(strpos($item, $findlink) !== false){
echo "<div class='cinemaLink'>" . $item . "</div>";
}elseif(DateTime::createFromFormat('d/m/Y', $item) !== FALSE){;
echo "<div class='cinemaDate'>" . $item . "</div>";
}else {
echo "<div>" . $item . "</div>";
}
}
echo "</ul>";
?>
This script however is outputting (with divs and classes of course, just don't want to add them all):
<li>The Smurfs 2 2D 30/08/2013 11:10</li>
<li>The Smurfs 2 2D 30/08/2013 13:20</li>
<li>The Smurfs 2 2D 31/08/2013 11:10</li>
<li>The Smurfs 2 2D 31/08/2013 13:20</li>
<li>Elysium 04/09/2013 15:45</li>
<li>Elysium 04/09/2013 21:00</li>
<li>Elysium 05/09/2013 15:45</li>
Is there any possible way to format it as follows:
<li>
The Smurfs 2 2D
30/08/2013 (today's date)
11:10 13:20
</li>
<li>
Elysium
04/09/2013 (today's date)
15:45 21:00
</li>
So just one film name, todays date and the times for that day (with classes ofcourse). Then onto the next film... etc.
I'm stuck and would appreciate any help! Thanks in advance.
Are you talking about the source layout or the front end html layout? If it's the source layout, have you tried replacing this:
echo "<div class='cinemaName'>" . $item . "</div>";
With this(?):
echo "<div class='cinemaName'>" . preg_replace( ' ', "\r\n", $item ) . "</div>";
Since it looks like there are 3 spaces between each segment of the $item, you can replace them with return shorthand \r\n (or just \n for some systems).
If it's the front end layout, just use the same method, but instead of \r\n, change it to <br/>.
I'm still a PHP noob, so I apologize if this is something simple.
I am creating a fairly basic search facility for a website using PHP and mySQL. I have connected to the database, selected the database, queried the table and have fetched the table columns;
$k = htmlspecialchars($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
while ($results = mssql_fetch_array($select)) {
$title = $results['Title'];
$link = $results['Link'];
$description = $results['Description'];
}
}
When I put the $results[''] columns into variables and then try to echo out each variable like so;
if( isset($noResults)) {
echo $noResults;
} else {
echo '<li>' . '<h2>' . '' . $title . '' . '</h2>' . '<p>' . $link . '</p>' . '<p>' . $description . '</p>' . '</li>';
}
it only echo's out one row matching that query however, If I was to just simple echo out the columns like so;
echo $results['Title'];
echo $results['Link'];
echo $results['Description'];
all rows matching the query will be displayed..
I'm not sure why this is happening. If someone could help me out that would be great!
You need to use a loop:
$k = mysql_real_escape_string($_GET['k']); // Get search query
$select = mssql_query("SELECT * FROM search WHERE Title Like '%" . $k . "%'");
if( mssql_num_rows($select) < 1) {
$noResults = 'No results found for <b>' . $k . '</b>, <label for="k">Please try again.</label>';
} else {
$results= array();
while ($result = mssql_fetch_array($select)) {
$results[]= $result;
}
}
if( isset($noResults)) {
echo $noResults;
} else {
echo "<ul>";
foreach($results as $result){
echo '<li>' . '<h2>' . '' . $result['title'] . '' . '</h2>' . '<p>' . $result['link'] . '</p>' . '<p>' . $result['description'] . '</p>' . '</li>';
}
echo "</ul>";
}
Do you execute the output in the while-loop?
If you execute the while-loop and call the echo after that, each resultset will overwrite the previous, and the echo will output the last resultset which was fetched.
If you call the echo in the Loop, every result set will generate "his own" output line.
If you want to hold every resultset in a variable you can use an array, which is declared in front of the loop and gets filled in the loop.
a few things are not clear from your question, but i am assuming that you are echo'ing the variables outside the loop since you are checking isset($noResults). that means you are reassigning the variables with new values in each loop of while. so ultimately you get the last one assigned to the variables. you have to either use an array to hold the values or echo it with in the loop.