How to pass an URL to a <a href=""> - php

I'm still new to php and I've made a script that checks the language of the website and if it's English set's the button link to an english page and else to another page.
Here goes:
<?php
$link1 = "http://www.uvt.ro/ro/accesibilitate/";
$link2 = "http://www.uvt.ro/en/accesibilitate/";
$url = "";
if($_SESSION["lang"]->lang!="ro"){
$url = $link2;
}
else {
$url = $link1;
}
?>
Button
It's supposed to be simple, I don't know why it doesn't work.

You'd echo the $url like so:
<?php
$link1 = "http://www.uvt.ro/ro/accesibilitate/";
$link2 = "http://www.uvt.ro/en/accesibilitate/";
$url = "";
if ($_SESSION["lang"]->lang != "ro") {
$url = $link2;
}
else {
$url = $link1;
}
?>
Button
You can condense the code to this using the ternary operator:
<?php
$link1 = "http://www.uvt.ro/ro/accesibilitate/";
$link2 = "http://www.uvt.ro/en/accesibilitate/";
$url = ($_SESSION["lang"]->lang != "ro") ? $link2 : $link1;
?>
Button

You have to echo $url in tag.
Button

Button
replace with:
Button
OR
Button

you can try this one:
See the page
<?php
$test = $_GET['p'];
?>
Test
or
Try like
HTML in PHP :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like
PHP in HTML :
Link

Related

How to increase/decrease the number in $_GET variable link?

I have a PHP page that utilizes a $_GET variable, "link", to display a specific image on the page. My PHP page URLs look like "website.com/page.php?link=1"
PHP Code:
$link = $_GET['link'];
if($link == 1) { $linkURL = "http://website.com/image1.jpg";}
if($link == 2) { $linkURL = "http://website.com/image2.jpg";}
if($link == 3) { $linkURL = "http://website.com/image3.jpg";}
HTML
<img src="<?=$linkURL;?>"></img>
I want to add 'Next' and 'Previous' buttons on this page, which will go to the next/previous $link URL when clicked.
<a class="prevBtn" href="">Previous</a>
<a class="nextBtn" href="">Next/a>
What should I put as the href in order to achieve this? Basically, I want it to be "$link -1" for previous and "$link +1" for next.
Thanks in advance for the help!
Assuming your images will always be image1.jpg, image2.jpg, image3.jpg etc, you can achieve what you're looking for like this:
Note: I used PHP only just so it's easier to follow the logic
<?php
//Check $_GET variable exists otherwise set it to a default value = 1
if(isset($_GET['link'])) {
$link = $_GET['link'];
} else {
$link = 1;
}
if($link == 1) { $linkURL = "http://website.com/image1.jpg";}
if($link == 2) { $linkURL = "http://website.com/image2.jpg";}
if($link == 3) { $linkURL = "http://website.com/image3.jpg";}
// Set variables for the previous and next links
$prev = $link-1;
$next = $link+1;
//Display your image
echo "<img src='".$linkURL."'></img>";
//Only show the previous button if it's NOT the first image
if($prev!=0){
echo "<a class='prevBtn' href='http://website.com/page.php?link=".$prev."'>Previous</a>";
}
//Only show the next button if it's NOT the last image
if($next<=3){
echo "<a class='nextBtn' href='http://website.com/page.php?link=".$next."'>Next</a>";
}
?>
You try can pass the parameters through url as
<a class="prevBtn" href="<?php echo $linkURL?>?link=-1">Previous</a>
<a class="nextBtn" href="<?php echo $linkURL?>?link=+1">Next/a>
An alternative way of doing this would be to use an array. The advantage of this is you can just add new images to the array and it will still work. You don't have to worry about editing the other if statements around the next button.
<?php
//Check $_GET variable exists otherwise set it to a default value = 1
$images = [
"image1.jpg",
"image2.jpg",
"image3.jpg",
];
if(isset($_GET['link']) {
$link = $_GET['link'];
} else {
$link = 0;
}
if(!$images[$link]) {
$link=0;
}
// Set variables for the previous and next links
$prev = $link-1;
$next = $link+1;
//Display your image
echo "<img src='http://website.com/".$images[$link]."'></img>";
//Only show the previous button if it's NOT the first image
if($prev<0){
echo "<a class='prevBtn' href='http://website.com/page.php?link=".$prev."'>Previous</a>";
}
//Only show the next button if it's NOT the last image
if($next!=count($images)){
echo "<a class='nextBtn' href='http://website.com/page.php?link=".$next."'>Next</a>";
}
?>

Getting Nothing from URL Parameters using $_GET

I've got this url: /admin.php?op=admin&action=delete&link=/showitems.php?id=8745
I need to get the op parameter and the action and the link parameters as well. If I code something like this:
$op = $_GET['op'];
echo $op; //the output is admin
$action = $_GET['action'];
echo $action; //The output is nothing;
$link = $_GET['link'];
echo $link; //The output is nothing;
//IF I want to sanitize the data same happens with
$op = filter_input(INPUT_GET,"op",FILTER_SANITIZE_STRING);
echo $op; //the output is admin
$action = filter_input(INPUT_GET,"action",FILTER_SANITIZE_STRING);
echo $action; //the output is nothing
$link = filter_input(INPUT_GET,"link",FILTER_SANITIZE_STRING);
echo $link; //the output is nothing
//If I go for the whole array of parameters there they are.
foreach($_GET as $key=>$value){
echo $key . " : " . $value; //The output is op : admin;action : deleteamp;link : /showitems.php?id=8745
}
what could it be the problem when I try to directly get the second or the third parameter like in this case action and link? Thanks for any advise. Greetings.

HTML Iframe get src with php

I have an webpage where I want to have another html page displayed. I used iframes. The page gets to know what to load is via the get procedure. But there is an fault in this coding I think...
<iframe src="
<?
$file = ($_GET['ti'])
if ($title = '')
echo "information.html";
else echo "$file";
?>
"></iframe>
The url the page would recieve looks like this:
http://www.website.com/reference.html?ti=unlimited.html
http://www.w3schools.com/php/php_if_else.asp
It's your if / else syntax and over all php code. It's not very well written.
<?php
$file = $_GET['ti'];
if ($title = '') {
echo "information.html";
} else {
echo "$file";
}
?>
Need semicolon:
$file = ($_GET['ti']);
Use empty(), like this:
<iframe src="
<?
$file = ($_GET['ti'])
if (empty($title))
echo "information.html";
else echo $file;
?>
"></iframe>
<?php
$possible = array("information.html", "home.html", "test.html");
$file = isset($_GET['ti']) &&
in_array($_GET['ti'], $possible)? $_GET['ti'] : "information.html";
?>
<iframe src="<?php echo $file;?>"></iframe>

add live ID to BODY with PHP

I'm using the php code:
<body id="<?php echo str_replace("index.php?","",(basename($_SERVER['REQUEST_URI'],".php"))); ?>">
and
<?php
if(isset($_GET['start'])){
include('includes/start.php');
}else if(isset($_GET['help'])){
include('includes/help.php');
}else{
include('includes/start.php');
}
?>
It works great - cutting index.php?help to "help" and index.php?start to "start" in body ID. But when I enter index.php in body ID is "index" not "start". Is there any way to tell index.php with included start.php to display "start" id body ID?
UPDATE
It should work dynamically - body ID is name of included .php file, something like this code:
<?php
$page = str_replace(array( 'server_name', 'index', '?', '/', '.php'), '', $_SERVER['REQUEST_URI']);
$page = $page ? $page : 'start';
?>
<body id="<?php echo $page ?>">
Am not sure i get you 100% but you can try
$id = $_SERVER['QUERY_STRING'];
$bodyID = $id;
switch ($id) {
case "help" :
include ('includes/help.php');
break;
default :
$bodyID = "start";
include ('includes/start.php');
break;
}
printf("<body id=\"%s\" >", $bodyID);
If you run index.php?help it would include include ('includes/help.php'); and also output
<body id="help" >
It would be best to use this instead:
<?php
$ids = 'start,help,something';
$ids = explode(',',$ids);
$included = 0;
foreach ($ids as $id) {
if (isset($_GET[$id])) {
include('includes/'.$id.'.php');
$included = 1;
break;
}
}
if (!$included) include('includes/'.$ids[0].'.php');
?>
Then:
<body id="<?php echo $id; ?>">

Parsing title and link from html page

I wanna get the title and link from this html page :
<div class="gs_r">
<h3 class="gs_rt">
<span class="gs_ctc">[BOOK]</span>
titleA
</h3>
<div class="gs_ggs gs_fl">
<a href="http://exampleA.pdf" onmousedown="return scife_clk(this.href,'gga','gga','1')">
How can I get them?
here's the code :
<?php
include 'simple_html_dom.php';
$url = 'http://example.com';
$html = file_get_html($url);
//get the first link
foreach($html->find('span[class=gs_ctc]')as $Link){
echo $link;
}
foreach($html->find('div[class=gs_ggs gs_fl]')as $docLink){
echo $docLink;
}
?>
For the first link, it's a sibling of the <span>. Try this:
//get the first link
foreach($html->find('span[class=gs_ctc]') as $link){
$link = $link->next_sibling();
echo $link->plaintext;
echo $link->href;
}
As for the 2nd link, it's a child of the <div>:
foreach($html->find('div[class=gs_ggs gs_fl]') as $docLink){
$link = $docLink->first_child();
echo $link->href;
}
EDIT: The 2nd link is grouped with the first, so you can try this:
foreach($html->find('span[class=gs_ctc]') as $link){
foreach($link->parent()->parent()->find('div[class=gs_ggs gs_fl]') as $docLink){
$link1 = $link->next_sibling();
$link2 = $docLink->first_child();
if(preg_match('/\.pdf$/i', $link2->href) === 1){
echo $link1->plaintext;
echo $link1->href;
echo $link2->href;
}
}
}

Categories