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.
Related
What I was looking for before was a way to dynamically create links for the same directory but have different URL variables, that way the same page works from the variables provided to load images from a folder. Below was my example code for loading all of the images inside the directory.
$dir = "./Ch.001/";
$a = glob($dir."*png");
foreach($a as $image){
echo "<img src='". $image ."'>";
}
?>
At the time I wasn't sure what to research to achieve what I wanted but thanks to an answer and some more experience in PHP, my answer to what I was looking for above would have been $_GET variables and urlencode()/htmlspecialchars().
Now I would use a database to store all of the information I wanted to display, so when the series is created from a form I would store the title, description, etc, and then calculate how many rows are in the database, and then make that number + 1 the file name, and maybe add a prefix so it isn't just numbers.
Presuming 001 is 001 to n etc.
Use a simple $_GET parameter, check or cast to int, 0 pad it and then show your links with +1 for next.
<?php
$chapter = str_pad((int) ($_GET['chapter'] ?? '001'), 3, "0", STR_PAD_LEFT);
//
$dir = "./Ch.$chapter/";
//
$images = glob($dir."*png");
if (empty($images)) {
echo 'Go back';
} else {
foreach($images as $image){
echo "<img src='". $image ."'>";
}
echo 'Chapter: '.$chapter.PHP_EOL;
echo 'Goto Chapter';
}
)
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;
}
I am using the code below for a Facebook feed I have on a website. Problem is that it lists the images and the news like it is supposed to but for the life of me, I can't figure out how to put the images on their and have the associated text displayed nicely to the right of it. Can someone please help.
Here is the code:
<?php
//Get the contents of the Facebook page
$FBpage = file_get_contents('https://graph.facebook.com/135860723149188/feed? access_token=456215171075777|OcFg4Sf293Pg3NXeJkg1h3Bg8wE'
);
//Interpret data with JSON
$FBdata = json_decode($FBpage);
//Loop through data for each news item
foreach ($FBdata->data as $news ) {
//Explode News and Page ID's into 2 values
$StatusID = explode("_", $news->id);
echo '<li class="fbframe"; style="list-style:none;" >';
//Check for empty status (for example on shared link only)
if (!empty($news->picture)) {
printf ('<img src="%s" alt="Image from Facebook" />', $news->picture);
}
if (!empty($news->message)) { echo $news->message; }
echo '</li>';
} //end of fbframe
?>
Thanks in advance for any help that you can offer.
wrap the <li> elements in an <ul style="list-style:none;">
remove the semi-colon after "fbframe";
Sounds like Nicole Sullivan's media object is exactly what you're looking for, Facebook and all.
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;
}
}