Display ALL data in a PHP switch on a single page - php

I have a PHP switch that holds brief articles about a certain topic for the 50 U.S. states. For example, the content under ID 'ak' displays on the Alaska page...
<?php
switch($ID)
{
case 'ak':
?>
Text
<?php
break;
case 'ca':
?>
Text
<?php
break;
default:
break;
}
I designed it so that I can see the text and work on it in "plain view."
It works fine, but I just wondered if there's a simple way to display EVERYTHING in the switch on a single page. It could easily be done if the data was stored in a database, of course, but I don't know how to do it with a simple PHP switch.
I can do it manually, like this:
$ID = 'az';
echo '<h1>Arizona</h1>';
require($BaseINC."../../inc/view.php");
require($BaseINC."../../inc/eco/plants/flower.php");
$ID = 'wy';
echo '<h1>Wyoming</h1>';
require($BaseINC."../../inc/view.php");
require($BaseINC."../../inc/eco/plants/flower.php");
But I just wondered if there's an easier way.
One catch: I don't want to "kill" the ID's. Each ID may influence some of content displayed beneath that ID. So I guess a better way would be to give the display file some sort of universal ID that displays everything.
Note: Just to explain why I'm doing this, I was using Sigil to create epubs, but discovered I could manage my projects better using PHP, so I now want to create epubs in Dreamweaver, then import them into Sigil. I'm basically trying to create a simple test page.

<?php
$states = ['ak' => 'Arizona', 'ca' => 'California', 'wy' => 'Wyoming'];
foreach ($states as $id => $stateName) {
echo '<h1>' . $stateName . '</h1>';
require($BaseINC."../../inc/view.php");
require($BaseINC."../../inc/eco/plants/flower.php");
}
You can foreach loop to do this.

Related

Print an array value when its key matches with a variable (PHP)

I have a "album.php" page with four albums; when you click on one of them it links to "gallery.php" showing the photos of that specific album.
Now I would like to put a specific title in "gallery.php" depending on the album clicked by the user and I would like to do it in a nice and clean way.
gallery.php:
$query_url = $_SERVER['QUERY_STRING'];
$pageID = substr($query_url, 6);
$album_title = array("album1" => "title1",
"album2" => "title2",
"album3" => "title3");
while($album_title[KEY] = $pageID)
{
echo '<div id="album_title">
<h2 class="body_text">'.$album_title[VALUE].'</h2>
</div>';
}
I thought something like that instead of the less dynamic:
if($pageID = "album1")
{echo "title1";}
if($pageID = "album2")
{echo "title2";}
etc.
My problem is that I don't know how I could "say":
when(array[KEY] = $pageID)
{
echo 'the correspondent array[VALUE]';
}
Thanks everyone!
[EDIT] Voting negatively without an explication doesn't solve any problem in this world, mate. Was probably 'cause I used "while" instead of "foreach" in my example? I know about its existence but I tried multiple times with it also without success.
Provinding $pageID is something like "album1" you wouldn't need any loop:
echo '<div id="album_title">
<h2 class="body_text">' . $album_title[$pageID] . '</h2>
</div>';
Check it out.

build link using postgresql and php

)
I have a question:
I have a databse with the following information:
Name of report [name]
Link to report [link]
The various reports are displayed in tables in my page (using db_tables filed with static, month, week etc)
But i want to generate a link to the report based on "link" and "name"
so i set up a pgsql query :
result = select name, link from db where type = 'week'
This works fine.
To display the above i have:
echo "<div id=\"selMaand\">" ;
echo "<table id = \"t2\">\n ";
echo "\t<tr class=\"head\">\n";
echo "<th colspan=\"2\">Select Maand</th>";
while ($line = pg_fetch_array($result5, null, PGSQL_ASSOC)) {
echo "\t<tr>\n";
foreach ($line as $col_value) {
echo "\t\t<td width=\"100%\">$col_value</td>\n";
}
echo "\t</tr>\n";
}
echo "</table>\n";
echo "</div>";
which works fine to.
Now, the link from database is http://192.168.178.1:8080/etc/etc/page.html"onclick humptydumpty>name
each time i want to adjust something in either the onlclick or address, i have to change all my links.
What can i insert in my querys so i will be able to build a link like;
echo "<a href = "http://192.168.178.1:8080/etc/etc/**$link**"onlclick humptydumpyu>**$name**</a>
I cant figure out how to break down my array!
Thanks in advance
Sjoerd
If you just need a portion of your URL in the database (i.e. only a portion of it would change for each record), then only store that portion of the URL in the database. You can then generate the remaining portion of the URL (call it the URL base) in your code.
So something like this:
define('URL_BASE', 'http://some.url.base/that/you/can/change/globally/');
Then you get the remaining piece of the URL from the database for each item. So you would have something like this inside your query loop to build the full URL.
$link = $line['link'];
$final_url = URL_BASE . $link;
As far as the onclick stuff goes, I would have that set sepearately as well, since I assume the onclick behavior is not dependent on the individual link (if it is you can make a separate DB column for it).
define('LINK_ONCLICK', 'onclick="some_onclick_function()"');
And in your query loop you can put this together like this:
$name = $line['name'];
$link = $line['link'];
$final_url = URL_BASE . $link;
echo '<a href="' . $final_url . '" ' . LINK_ONCLICK . '>' . $name . '</a>';
Note that it is not really necessary to define the strings for the url base and onclick behavior as constants. I just showed it this way as it is common practice to set globally defined values that do not need to change at run-time ion such a manner.

Dynamic PHP Content via Query Strings

I'm wanting to use a pair of query strings to generate specific page content
query string example:
example.co.uk/index.php?brand=apple&video=red
I've managed to use some simple PHP to replace the div id's with query brand with some help as follows:
<?php
$recognised_brands = array(
"apple",
"orange",
// etc
);
$default_brand = $recognised_brands[0]; // use first from list arbitrarily.
$brand_to_use = isset($_GET['brand']) && in_array($_GET['brand'], $recognised_brands)
? $_GET['brand']
: $default_brand;
echo '<div id="' . $brand_to_use . '"></div>';
?>
However I'm unsure on how to do similar for the second query called video.
If for example they query is ?brand=apple&video=red
Then I'd want to replace the video var in the page code to the correct number for red but I don't know how to add a list of these variables based on the video query like: red => 1 which then echo's 1 into the correct area?
Also based on the video selected I'd like part of the heading text to change and a paragraph of code below it but also unsure on where is best to specify this:
To try and clarify:
I have my brand query sorted
I need to make my video query populate multiple areas headings, video id and content div's but not sure how!
If I understood your question correctly, this would do what you are looking for:
$videoToNum = array("red" => 1,
"blue" => 2,
"yellow" => 3);
$videoValue = ((isset($_GET['video']) && isset($videoToNum[$_GET['video']])) ? $videoToNum[$_GET['video']] : null);
if($videoValue) {
echo '<div id="' . $videoValue . '"></div>';
}
The question is rather vague though, so you may want to elaborate further if this doesn't help.
Edit: Here's an example of a switch as discussed in the comments:
$video = (isset($_GET['video']) ? $_GET['video'] : null);
if($video) {
switch($video) {
case "red":
//do red stuff
break;
case "blue":
//do blue stuff
break;
case "yellow":
//do yellow stuff
break;
}
}

Wordpress meta tag challenge

I'm currently trying to create a site for TV shows and due to certain wordpress limitations this is becoming a challenge.
However I bypass that with the use of implementing custom meta fields in the functions.php file, now my problem is that I need it to actively create new fields when I submit information in the current field. For example custom metabox names are
(Episode Name="This is It") (Episode Number="1") (Season Number="5")
Instead of having to create all the boxes from the beginning I would like the use Javascript (jQuery) or any solution to automatically create a new set of these 3 boxes
(Episode Name="") (Episode Number="") (Season Number="")
so I can just enter the new information as they come. Thank you in advance for your help!
Note: I have invested too much time into wordpress to just switch to another cms, so that is not an option at this point in time.
from what I understand of your question, you are looking for a simple solution to automate the input process. I have a general idea of what it is you nee to achieve as I have had to do something similar on a brochure type of website.
I have tried answering your question using Jquery, but I find it to increase the amount of text input required when creating your post, there is unfortunately no completely automated method of doing it, but hopefully below would provide you with a solution.
first I found the following plugin: Types - Complete Solution for Custom Fields and Types here: http://wordpress.org/extend/plugins/types/
This allows you to create custom meta feilds when creating a new post/page. The custom feilds are brought added with a perfix of "wpcf-" and then the name of the field, e.g. "Episode Name" becomes "wpcf-episode-name" in the database.
The following is an edit of wordpress's get_meta function:
function get_specifications(){
if ( $keys = get_post_custom_keys() ) {
echo '<div class="entry_specifications">';
foreach ( (array) $keys as $key ) {
$keyt = trim($key);
if ( '_' == $keyt[0] )
continue;
$values = array_map('trim', get_post_custom_values($key));
$value = implode($values,', ');
//remove "wpcf-"
$key = str_replace('wpcf-','',$key);
//convert "-" to a space " "
$key = str_replace('-',' ',$key);
//check if it has a value, continue if it does, skip if it doesn't:
if ($value <> ''){
echo apply_filters('the_meta_key', "
<div class='meta_wrapper'>
<div class='meta_title'>$key</div>
<div class='meta_value'>$value</div>
</div>\n", $key, $value);
};
}
}
// echo '</div>'; comment out and remove the line below if you are not using floats in your css
echo '</div><div class="clear"></div>';
}
In my page.php (or your template-page.php) I have added the following code when/where I want the meta to be produced:
<?php
//check to see if there is meta data, as you only want the meta data on TV Program pages
$met_check = get_post_meta($post->ID, 'wpcf-features', true);
if ($met_check <> ''){
//if it has a value, spit out "About EpisodeName" ?>
<h2 class="post-title clear">About <?php echo $title ?></h2>
<?php //perform the function from functions.php and return results:
echo get_specifications();
}
?>
I've styled the result with the following CSS:
.meta_entry {
clear:both;
}
.meta_title {
float:left;
text-transform:capitalize;
width:200px;
}
.meta_value {
float:left;
width:480px;
}
Hope this helps mate!
There is a wonderful plugin for wordpress called Pods which might be a viable solution.
Try http://wordpress.org/extend/plugins/custom-field-template/

Iterating through a foreach loop - only show 1 item rather than all

I've setup a Google map that has several plotted markers, each one when clicked on has a little popup with info within.
I'm using the following code to show the company name per plotted marker, but it's going through the foreach loop and showing ALL the company names within one popup.
How can loop through and show one company name per plotted marker? I need it to show the first company in the list on the first marker, the second company in the list on the second marker and so on.
// Setting the content of the InfoWindow
infowindow.setContent('
<?php $pages = get_pages(array('child_of' => $post->ID, 'sort_column' => 'menu_order')); foreach($pages as $post) {
setup_postdata($post);
$fields = get_fields(); ?>
<p><?php echo $fields->company_name; ?></p>
<?php } wp_reset_query(); ?>
');
Look at your code. what a mess! You would never have missed these obvious mistakes, if you would care to structure your code well! Don't write all your code on one line! Use best practice for code style!
Don't put all that PHP code inside the javascript function.
Instead, use a variable $contentMarkup, store everything in there and in the end echo this variable into the javascript code.
<?php
$contentMarkup = '';
//do all your stuff like
$contentMarkup .= '<p>';
$contentMarkup .= $fields->companyName;
$contentMarkup .= '</p>';
?>
infowindow.setContent('<?php echo $contentMarkup; ?>');
Regarding your actual question: If you want to generate more than one tooltip/window/younameit, they have to have unique identifiers so you can create one for each company. But to say more I'd need to know a bit more about what exactly you're trying to do. What information is available and from what source.
show what you want based on if statement inside foreach loop

Categories