i must pass multiple link as variables on php
ex: www.mysite.com/dl.php?link=www.google.com&link2=yahoo.com&link3=youtube.com and so on, there is a variable number of links, and then i want to put them on and html page generated dynamically based on the number of links i inputed, in the example i did, the links was 3, so it must be:
<html><center>
Click to download part 1
Click to download part 2
Click to download part 3
</center></html>
can someone help me with this problem?
Parameters you append on your URL can be accessed through $_GET in php.
Take a look at this page: http://php.net/manual/en/reserved.variables.get.php
Update: If you have a variable number of get parameters and you want to get them all just use a foreach loop:
foreach($_GET as $key => $url) {
echo $url;
}
Instead of using different parameter names for all urls, use an array:
www.mysite.com/dl.php?link[]=www.google.com&link[]=yahoo.com&link[]=youtube.com
Then, in dl.php, $_GET['link'] is an array. You can iterate like this:
for ($i = 0; $i < count($_GET['link']); ++$i) {
echo 'Click to download part ' . ($i + 1) . '';
}
If URL = www.mysite.com/dl.php?link1=www.google.com&link2=yahoo.com&link3=youtube.com
<?php
for($i = 0; $i < count($_GET); $i++)
{
?>
Click to download part <?php echo ($i+1);?>
<?php
}
?>
If the only get value that is the URLs then just loop through...
foreach ($_GET as $url)
{
echo $url
}
Related
I am writing a web and i got stuck. PHP is not my language of choice, so I'm not that used to it. But lets look at the problem:
$i = 1;
$n = 0;
<div class="hs_client_logo_slider">
<?php
foreach ($hashone_client_logo_image as $hashone_client_logo_image_single) {
foreach ($hashone_logo_list as $hashone_logo_url) {
if ($i > $n) {
?>
<a target="_blank" href="<?php echo esc_url($hashone_logo_url) ?>">
<img alt="<?php _e('logo','hashone') ?>" src="<?php echo esc_url(wp_get_attachment_url($hashone_client_logo_image_single)); ?>">
</a>
<?php
$n = $n + 1;
continue 2;
} else {
$i = $i + 1;
continue 1;
Basically I am trying to do:
Foreach (x as y) && Foreach (a as b) {
/* Magic happens here */
In the first foreach I have images and in the second one links. And for the first image I need the first link stored in logo_list, for the second image I need second link and so on... So I tried If statement that could take care of it, but for some reason it doesn't work.
Now it gives me first image with first link, but every image after that is stuck with second link. Do you have any idea what to do?
I'm glad for everyone who tries to help me out. Thank you very much.
Try a different approach. First of all, prepare your data before passing it to foreach loop that is responsible for generating view, then your foreach loop will be much easier to read and maintain.
If every 1st element should go with 1st element in the second foreach, 2nd to 2nd, 3rd to 3rd and so on, then using two foreach loops is redundant. Use only 1 foreach and in every foreach iteration take its index (it can be a separate incremented variable) and take element from second array by index. You can also use a standard for loop and it should be even simpler to implement.
I am trying to use Parsedown Extra with Parsedown (Have never used either before). I have the code $_GET the selected category (?cat=0) and set it's path & filename to a var. It $_GETs the page number just fine, however when I set the file var, it just prints to the screen and doesn't load my page.
//sets the page (category) number for use with array
//also sets the path to the category's pages
if (isset($_GET['cat'])) {
$catNum = $_GET['cat'];
$catPath = 'content/' . $pageList[$catNum]['path'];
echo '<div class="center pageNav">';
//lists out subpages of catagory
$pageAmt = count($pageList[$catNum]['pages']);
for ($i = 0; $i < $pageAmt; $i++) {
echo '' . $pageList[$catNum]['pages'][$i]['title'] . '';
};
echo '</div>';
//sets path & filename var to selected page: this is the part where it prints the var and doesn't run the rest. The var is pointing to the right file, I checked.
$page = $catPath . $pageList[$catNum]['mainPage'];
} else {
$page = 'content/home.md';
};
//parsedown
require 'parsedown/parsedown.php';
require 'parsedown/parsedownextra.php';
echo ParsedownExtra::instance()
->setBreaksEnabled(true)
->setMarkupEscaped(true)
->text($page);
you have a semi colon at the end of your if statement.
} else {
$page = 'content/home.md';
}; <--
Parsedown takes marked up text and renders it. In your example, you pass $page (which holds a string, a filename) to ->text($page). This parses the string as marked up text, and renders it. So, in your example you are seeing exactly what it is doing. If you are trying to run the text of the file through ->text, you need to load the file contents first and pass to Parsedown.
I'm trying to display all the images with the same key from a folder.
the images are stored like so:
34526Image1.jpg
34526Image2.jpg
34526Image3.jpg
34526Image4.jpg
etc etc....
so the key is the first part of the image name (the random numbers) which in this case is 34526.
I've tried to use glob() function in PHP but I only get the first image which is 34526Image1.jpg.
my glob() code is this:
<?php
foreach(glob('../my-images/') as $image)
$i = 1;
{
$pic_list .= '<a id="example1" href="'.$image.''.$randKey.'Image'.$i++.'.jpg"><img alt="example1" src="../my-images/'.$randKey.'Image'.$i++.'.jpg" /></a>';
}
echo $pic_list ;
?>
could someone please advise on this?
Your call to glob contains no pattern. You can use wildcards for no or many characters with *, or for single characters using ?. Also ranges like for example [0-9] are possible. In your case you want everything that starts with a random number, determinted by $randKey, followed by the word Image, a counter value and .jpg. So all you have to do is to use a wildcard for the counter value like in your example 34526Image*.jpg.
This results in the following code
<?php
$pic_list = '';
$id = 0;
foreach(glob('../my-images/'.$randKey.'Image*.jpg') as $image) {
$pic_list .= '<a id="example'.++$id.'" href="'.$image.'"><img alt="example'.$id.'" src="'.$image.'" /></a>';
}
echo $pic_list;
?>
you have error in your foreach statement, for each file in folder you execute only 1 code line $i = 1;, everything after ; is executed only one time, after all iterations of foreach
you need to use proper foreach:
$files = glob('../my-images/'); // need to define proper mask here to get only files with $randKey
$i = 1;
foreach($files as $image)
{
$pic_list .= '<a id="example1" href="'.$image.''.$randKey.'Image'.$i.'.jpg">';
$pic_list .= '<img alt="example1" src="../my-images/'.$randKey.'Image'.$i.'.jpg" />';
$pic_list .= '</a>';
$i++;
}
I am attempting to make a website that will load a certain menu.xml doc, that was sent to it using a foreach loop. The foreach loop uses glob to grab all XML docs in a directory then prints the "name" attribute into a html link.
This has worked to up until the point were I need to pass the certain XML var used to make the link to another php doc that utilizes it.
I first attempted to use the $_SESSION to but could not as the loop would overwrite $_SESSION with each iteration. I also tried $_GET and $_POST, but I ran into the same problem with forms.
Here is my code (Please ignore any sloppyness, I am new to the PHP game):
foreach(glob("../menus/*xml") as $dom) //grabs each .xml doc in menus/
{
$menu = simplexml_load_file($dom); //loads the file into $menu
print "<li>";
print "<a href = menu.php>{$menu["name"]}</a>"; //links to page menu.php
print "</li>";
}
What I need this to do now, is to pass the individually loaded XML variable when the link is clicked.
How could I go about doing this?
Edit:
menu.xml is a page that displays the contents of the menu.xml.
Something like this?
<?php
foreach(glob("../menus/*xml") as $dom) //grabs each .xml doc in menus/
{
$menu = simplexml_load_file($dom); //loads the file into $menu
?>
<li>
<a href="menu.php?name=<?php echo $menu["name"] ?>><?php echo $menu["name"] ?></a>
</li>
<?php
}
?>
The variable will be set into $_GET['name'] when the page is loaded.
You can store the DOMs in an associative array:
$xml = simplexml_load_file($dom);
$arr_item = array($xml["name"] => $xml);
$menu[] = $arr_item;
create the form line:
print "<a href = menu.php id='{$xml["name"]}' name='{$xml["name"]}' >{$xml["name"]}</a>";
and then when you want to call it, you can use its name to get the $dom:
$dom_obj = $menu["name"];
I'm using PHP to create a pagination for a table.
I'm using the following code to create the pagination link
<a class='page-numbers' href='$href&pagenum=$i'>$i</a>
With $href
$href = $_SERVER['REQUEST_URI'];
It works well, however, it messes with the address bar, adding each time a new pagenum parameter.
So it becomes pagenum=1&pagenum=3&pagenum=4....
How to improve that?
How about this? Went and tested, to be sure :)
<?php
$new_get = $_GET; // clone the GET array
$new_get['pagenum'] = $i; // change the relevant parameter
$new_get_string = http_build_query($new_get); // create the foo=bar&bar=baz string
?>
<a class="page-numbers" href="?<?php echo $new_get_string; ?>">
<?php echo $i ?>
</a>
Also, note that the whole $href bit is unnecessary. If you start your href with ?, the browser will apply the query string to the current path.
I bet you're going to be looping, though, so here's a version optimized for producing 10,000 page number links. My benchmarks put it as being ever so slightly faster at large numbers of links, since you're just doing string concatenation instead of a full HTTP query build, but it might not be enough to be worth worrying about. The difference is only really significant when there five or six GET parameters, but, when there are, this strategy completes in about half the time on my machine.
<?php
$pageless_get = $_GET; // clone the GET array
unset($pageless_get['pagenum']); // remove the pagenum parameter
$pageless_get_string = http_build_query($pageless_get); // create the foo=bar&bar=baz string
for($i = 0; $i < 10000; $i++):
// append the pagenum param to the query string
$page_param = "pagenum=$i";
if($pageless_get_string) {
$pageful_get_string = "$pageless_get_string&$page_param";
} else {
$pageful_get_string = $page_param;
}
?>
<a class="page-numbers" href="?<?php echo $pageful_get_string; ?>">
<?php echo $i ?>
</a>
<?php endfor ?>
$url = $_SERVER['REQUEST_URI'];
$urlparams = parse_url($url);
if(isset($urlparams['query']){
parse_str($urlparams['query'],$vars);
$vars['pagenum'] = $i;
$urlparams['query'] = http_build_query($vars);
} else {
$urlparams['query'] = 'pagenum='.$i;
}
$url = http_build_url($urlparams);
//http_build_url() is in PECL, you might need to manually rebuild the
//url by looping through it's components:
/*
$url=(isset($urlparams["scheme"])?$urlparams["scheme"]."://":"").
(isset($urlparams["user"])?$urlparams["user"]:"").
(isset($urlparams["pass"])? ":".$urlparams["pass"]:"").
(isset($urlparams["user"])?"#":"").
(isset($urlparams["host"])?$urlparams["host"]:"").
(isset($urlparams["port"])?":".$urlparams["port"]:"").
(isset($urlparams["path"])?$urlparams["path"]:"").
(isset($urlparams["query"])?"?".$urlparams["query"]:"").
(isset($urlparams["fragment"])?"#".$urlparams["fragment"]:"");
*/