a newbie question!
I have set up some code to create an image with this url:
www.mywebpage.php?wc1=99&wc2=6&wc3=23
In a php page want to use the wc1, wc2 and wc3 variables from the url as inputs to a bar chart graphic
the code on the receiving webpage includes this line:
$datay=array($_REQUEST['wc1'], $_REQUEST['wc2'], $_REQUEST['wc3']);
and using the datay array I get a nice bar chart with three bars.
My problem is I want to create urls with any number of wc? variables, and then create an array with any number of bars in the resulting bar chart. The numbero f bars should be dynamically set by some other real-time process.
So I realise I need to embed this line of code:
$datay=array($_REQUEST['wc1'], $_REQUEST['wc2'], $_REQUEST['wc3']);
in a loop and add in variablles to the array one by one until they are all added (whatever number there are).
However, I am stuck at the first hurdle as I do not know how to add a variable to the array.
This code does not work:
$firstBit = $_REQUEST['wc1'];
$datay=array(firstBit, $_REQUEST['wc2'], $_REQUEST['wc3']);
You're just missing the $ from your variable name
$datay=array($firstBit, $_REQUEST['wc2'], $_REQUEST['wc3']);
foreach($_GET as $key=>$value){
echo $key;
echo "<br/>";
echo $value;
echo "<br/>";
}
You'll soon understand
In your request, pass the values thus:
www.mywebpage.php?wc[]=99&wc[]=6&wc[]=23
And then you have an array out of the box...
$myArray = $_REQUEST['wc'];
Related
I know there must already has this question been asked but I didn't find the answer cause I don't know how to search exactly what I want.
So, I wanna make such a link http://example.com/index.php?cat=about where I should insert some about info. But I don't know how to make a page with this URL containing ? symbol and other stuff, or how to edit that page then, where to edit and etc.
About Us
I've also made cat.php file but what next?
In your index.php file you can use the following:
if(isset($_GET['cat']) { // means if the user use the url with ?cat=something
echo "<1>About {$_GET['cat']}</h1>"; //print the about of the cat as html
}
OK, Suppose you've two PHP files. page_a.php & page_b.php.
page_a.php
<?php
echo "<a href='page_b.php?cat=about'>Click Me</a>";
page_b.php
<?php
print_r($_GET); // Show all GET contents
echo $_GET['cat']; // Show what content exist in 'cat' part of url
Hope, this will clear your doubt of how to send data in url from one page to another using GET mehtod.
To store variable data in the url, you can use the query string. This is the portion of the url that immediately follows the protocol, domain name and file path
The query string begins with ? and may contain one or more parameter parameter value pairs. The parameter and parameter value are separated by =. Each pair is separated by &.
Let's suppose you do development for a tourism company called Gi Tours which provides content in 3 different languages. Because site visitors will want to be able to select their preferred language, you can provide flag images that will indicate a certain language and wrap those flags with the appropriate hyperlinks. Rather than writing out the full language name, you can simply assign id numbers to represent each like this:
<?php
echo "<img src=\"img/ge.png\">";
echo "<img src=\"img/en.png\">";
echo "<img src=\"img/ru.png\">";
?>
If a visitor clicks the second flag which loads this url: https://www.gitours.ge/index.php?lang=2, your index.php code can be written to extract the value assigned to lang by using $_GET["lang"].
If you write in your index.php file:
<?php
echo $_GET["lang"];
?>
Your code will display:
2
Or on your index.php file, you can easily generate dynamic page content using $_GET array data.
<?php
if(isset($_GET["lang"])){ // this checks if lang exists as a parameter in the url
$lang=$_GET["lang"]){ // $lang will equal the value that follows lang=
}else{
$lang=1; // if there was no lang parameter, this sets the default value to 1
}
if($lang==2){
// show English content
}elseif($lang==3){
// show Russian content
}else{
// show Georgian content
}
?>
This is, of course, a simplified demonstration; other techniques can be used to interact with the $lang value.
#Lasha Palelashvili let suppose below example:
above you are sending only one input parameter cat through url to the index.php file at server side so when you send this data from url it will send by get method by default, if you want to get this url info(input parameter) at the php side so $_GET will help you to fetch that info $_GET is actually an array which stores your input parameter as key and input parameter's value as value for your case("index.php?cat=about") $_GET array will contain value like below:
$_GET = array("cat" => "about")
now at the server side you can easily get the value like:
//index.php
<?php
$cat = $_GET["cat"];
echo $cat;
?>
So i am making a simple forum to learn some PHP with CodeIgniter, by simple i mean 1 page with posts and you can click on them to comment and view more info(Think of reddit). All the data is stored in a mySQL database. Anyways i got all the links to display on my page, but what i cant figure out is how to open a new page to show the description and comments of the post. I remember doing something similar with a long time ago, can't remember how i did that sadly.
<?php
foreach($records as $rec){
$test = $rec->PostName."<br/>";
Echo "<a href=#$test>$test</a>";
}
?>
<?php
echo '<div data-role="page" id="$test"></div>';
echo "THIS ISSSSS $test";
?>
So this is the part where i need help. Any suggestions are greatly appreciated
Well for starters you'll need to refactor your attempt at generating the links as that has issues as everyone has pointed out.
So I took the liberty to come up with some test code to demonstrate a few things.
So this is Part 1.
<?php
// Simulated Database results, an array of objects
//
// These may already be Slugs with First letter upper case, hyphenated between words
// But I will do that when generating the links.
$records = [
(object)['PostName'=>'Why I recommend reading more PHP Tutorials'],
(object)['PostName'=>'Why I should take heed of what others suggest and actually try them out before dismissing them'],
(object)['PostName'=>'What is the difference between using single and double quotes in strings'],
(object)['PostName'=>'Why everyone should know how to use their browsers View Source tool to inspect the generated HTML'],
];
foreach ( $records as $rec ) {
$test = $rec->PostName;
// This would make a good helper but Codeigniter might have something already.
// So you should go and read the manual.
echo "$test";
// The above uses " to wrap the string with escaped \" within the string
// cause you cant have both.
echo '<br>';
}
// This is only here for testing... I think.
echo '<div data-role="page" id="'.$test.'"></div>';
// The above uses single quotes to wrap the string.
echo "THIS ISSSSS $test";
Part of Part 2...
<?php
// Simulated Database results, an array of objects
//
//
// These may already be Slugs with First letter upper case, hyphenated between words
// But I will do that when generating the links.
$records = [
(object)[
'id'=>1,
'PostName'=>'Why I recommend reading more PHP Tutorials'],
(object)[
'id'=>2,
'PostName'=>'What is the difference between using single and double quotes in strings'],
];
foreach ( $records as $rec ) {
$name = $rec->PostName;
$id = $rec->id;
// The slug of the page isn't really being used here as
// we are providing the pages id for lookup via an AJAX Call.
echo "<a id=".$id." href=\"#".ucfirst(str_replace(' ','-',$name))."\">$name</a>";
echo '<br>';
}
// This is where the AJAX call fired by a click on an anchor, that sends the id
// which results in the fetching of the required details to plonk in our page-area div.
echo '<div class="page-area"></div>';
There are a number of ways to do this, but going totally basic, if we create a JS event for a click on an anchor.. Which fires an AJAX Call which posts the page id to our method that returns back the HTML of the results we want to display...
I am trying to submit an html form to a php server. However, this form can have a variable length and structure (I add input nodes through dynamic javascript as the user interacts with the page, depending on his actions). How can I pick up the values contained in the form from PHP (using $_POST['xxx']) if I don't know the structure of the form?
To loop through each of the POST values, use:
foreach($_POST as $key => $value) {
echo "POST parameter $key has $value";
}
Just a fun (but valid) answer, existing answer on this question is good enough.
if I don't know the structure of the form?
Then you can simply review
print_r($_POST);
and you'll get to know the structure of what was POSTed
To find the structure of the POST OR GET OR SESSION values, you can print using
echo "<pre>"; print_r($_POST); print_r($_REQUEST); print_r($_SESSION);
You can find easily what exactly printing/structure of POST/REQUEST/SESSION.. etc
echo '<pre>';
print_r($_POST);
echo '</pre>';
Easy and clean.
I'm trying to make the following form's GET function to be part of a predefined variable.
Any ideas? Thanks in advance!
Let me explain a little more of what I'm really trying to do. I currently run a website concentrating on the U.S. stock market. I've created an HTML form with a method=GET. This form is used like a search box to look up stock ticker symbols. With the GET method, it places the ticker symbol at the end of the URL, and I created a quotes.php page that captures this information and displays a stock chart based on what ticker symbol is keyed into the box. For the company names, I've created a page called company.php that declares all of the variables for the company names (which happens to be a $ followed by the ticker symbol). The file, company.php, is the only file included in quotes.php.
This is where this came in: ' . $$_GET["symbol"] . '
The above code changes the GET into the variable based on what was typed into the form. I've used "die" to display an error message if someone types something into the box that doesn't match a variable in the company.php page.
I've also added into the company.php page variables for each company that will display which stock exchange each stock is listed on. These variables begin with "$ex_". So, what I was trying to do was have the symbol keyed into the box appended to "$ex_" so that it would display the corresponding stock exchange.
My questions are:
Is there a way to have what is typed into the form added to "$ex_"?
Is this an insecure way to code something like this (can it be hacked)?
Thank you all!
Rather than prefixing your variables and using variable variables (that are potentially insecure especially with user input), try this:
$ex = array(
"foo" => "bar",
...
);
if( !isset($ex[$_GET['symbol']])) die("Error: That symbol doesn't exist!");
$chosen = $ex[$_GET['symbol']];
Here's another approach:
extract($_GET, EXTR_PREFIX_ALL, "ex");
Although it's better to use it like this just to make sure there is no security issues.
extract($_GET, EXTR_SKIP);
PHP's extract() does what exactly what you want, and you should specify "ex_" as the prefix you want.
However, there are security issues and unintended consequences to using such a function blindly, so read up on the additional paragraphs following the function parameters.
Will the below achieve what you need?
$myGetVariable = $_GET['symbol'];
$ex_{$myGetVariable} = "Something";
$_GET['symbol'] = 'APPL';
if (!empty($_GET)) {
foreach ($_GET as $k => $v) {
$var = 'ex_'.$k ;
$$var=$v;
}
}
var_dump($ex_symbol);
APPL
piece of file1.php:
Add this to DB
This takes the user to a page where the data gets inserted into the database.
file2.php :
$clicked = $_GET['addid'];
$_SESSION['clicked'] = $clicked;
// data gets inserted
header("Location: file1.php?id=$clicked");
But I have got multiple pages (like: file1.php?id=1 | file1.php?id=2 | file1.php?id=3 etc.).
Can the session variable handle multiple numbers? Is there any way to do this?
Any help appreciated.
(P.S.: Currently I am using the GET method to disable the links, but I think SESSION is more reliable.)
(P.P.S.: I need this for a voting script.)
To hold more data in one session variable, you need to create a multi-dimensional array which will hold multiple against $_SESSION['checked']. You can do this like:
$clicked = (int)$_GET['addid'];
$_SESSION['clicked'][$clicked] = true;
// data gets inserted
header("Location: file1.php?id=$clicked");
(also, you should be sanitizing $_GET['addid'].
Then to check if it is set, you can use array_key_exists:
if(array_key_exists($clicked,$_SESSION['clicked'])){
echo "this button should be disabled!";
}
I am not sure I understood right your question, but if it is how to send an array of data with the same id via an http request you can use this syntax for the url
file.php?arr[]=val1&arr[]=val2&arr[]=val3
from your php code you would access the values as a regular array
Can the session variable handle multiple numbers? Is there any way to do this?
The session variable can store an array
$_SESSION['clicked'] this session variable can store one value at a time.
If you want use 2 dimension array to handle multiple values.
$clicked = $_GET['addid'];
Ex: $_SESSION['clicked'][$clicked];
firstly concatinate all the ids with comma in a string as $var = 1,2,3,4 and then pass it using GET.
Then there on that page you can explode it from comma and can store it in array and then foreach loop for the array will do it for you. Hope it works.