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;
}
}
Related
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.
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.
I am a rookie with PHP, so here's my question.
I want to add a custom marker ICON on GoogleMap to each category.
Here what I want to do: icon:
"if php listing-type=6 ?>/images/red-marker.png"<br/>
"if php listing-type=7 ?>/images/blue-marker.png"
The code I have:
options: {
icon : "<?php echo get_template_directory_uri(); ?>/images/red-marker.png"
},
Here it´s the full code:
http://descubrime.com.ar/fullcode.txt
And here its the site:
http://descubrime.com.ar/
I am completly lost!
I am not clear on what you are asking, but I am almost sure you want something like this. Please alert me if you were asking something else.
Since you are a beginner, I will try and explain myself.
<?php
$parentDirectory = get_template_directory_uri();
$listingType = getListingType();
$icon = ""
if($listingType == 6)
{
$icon = $parentDirectory . "/images/red-marker.png"
}
else
{
$icon = $parentDirectory . "/images/blue-marker.png"
}
?>
options: {
icon : "<?php echo $icon; ?>"
},
Your basic question is to inject the path of your icon dynamically in your javascript. So anywhere before the options line, you start to declare the above php code and declare your parentDirectory and the listing type. I have used a method getListingType() which can be anything to get the listing (6, 7 whatever). Then based on the type I prepare my $icon variable which contains the dynamic path to your actual file.
)
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.
I am working on building a news section for one of my sites. I am storing the information for each news article in a database table news, the main text of the article is in column copy whose type is set to longtext.
I use the following line of code to display the news article as paragraphs:
<p class="news_copy"><?php echo preg_replace('/[\r\n]+/', '</p><p class="news_copy">', $news['copy']); ?></p>
and it works absolutely perfect.
The reason for this post is because I would like to be able to display more than one photo for each news article. I am setting a lead image in the database, but if there were, say, 4 other photos in one story and 6 in another (in addition to the lead image) is there any way for me to display, say, 1 or more photo(s) per paragraph?
I'll be happy to elaborate in case this is too confusing.
Many thanks!
<?php echo insert_par_and_photos($news['copy'], $news['photos']); ?>
function insert_par_and_photos($copy, $photos) {
$pars = preg_split('/[\r\n]+/', $copy);
$result = '';
foreach ($pars as $i => $par) {
$result .= '<p class="news_copy">';
$result .= $par;
if ($i < count($photos)) {
$result .= '<img src="'+$photos[$i]+'"/>';
}
$result .= '</p>';
}
return $result;
}