how to pass values from a php script to another dynamically? - php

i want to pass a url value from a php script to another.I have a database in which i have stored some feeds.I have given some weight values to these feeds and a php script grabs a feed url randomly based on their weights.I want to take the feed url which has been grabbed by the script and then pass this url in another php script where it will be parsed with simplepie in order to show its content.
I am posting the codes of the two files right here:
this is the first script which grabs randomly the feed
http://pastebin.com/2ciQ87Es
this is the second script in which i want to pass the value and makes the parsing of the feed
http://pastebin.com/eN5qG29e
Have you something to recommend??
thanks in advance

Would a $_SESSION not suffice?
In the first script:
session_start();
$_SESSION['session_name'] = 'value';
In the second script:
session_start();
print $_SESSION['session_name'];
On second thoughts, could you not pass the value in a query string, to the second page.
second-page.php?key=value

You could wrap grabbing the feed url from the database in a function, and just include that file like any other php file, and then call that function.
//// feedgrabber.php
<?php
function grabber(){
$query = "SELECT * FROM `feeds`";
//takes all the feed that are declared
$result = mysql_query($query);
$data = array();
while($output = mysql_fetch_assoc($result)) {
$data[] = $output; // assigns feeds to an array called $data, one after the other, in they go!
}
return randomchoice($data); // finds a random feed by calling the function
}
?>
and then on the page where you need it:
require('feedgrabber.php');
$feed = grabber();

Related

Can't combine two strings properly

I'm trying to assign the return value of a php script to a js variable. I have this:
var email = jQuery("input[name=email]").val();
var emailRegex = /^([\w-\.]+#([\w-]+\.)+[\w-]{2,})?$/;
var exists='<?php
$query = "SELECT * FROM rss_members WHERE email_id=\"something#email\"";
$results = mysql_query($query);
$results = mysql_num_rows($results);
echo $results;
?>';
console.log(exists)
The query works and I get the correct results back, but I want to replace "something#email.com" with the email variable, but if I write something like email_id=\"'+email+'\"..., the query result comes back incorrect. What am I doing wrong?
You aren't taking in consideration execution time.
When you make a request to a server (enter www.google.com for example) the server gets a request, and replies with a HTML page. The server in your case is in PHP and it sends a HTML page with some javascript included in it. After the browser receives the HTML page, it then interprets it, and run javascript code.
So basically, your php code ran without access to the email variable. If you wanted to have access to server-side information after the page loaded, you would need to make an ajax request

XML loading very slow with PHP

I have the following code:
<?php
include "convert/xmlToArray.php";
$query_string = $_GET['query_string'];
if ($query_string == "") {
$query_string = "travel";
}
$completeurl = "http://my_site";
$xml = simplexml_load_file($completeurl);
$arrayData = xmlToArray($xml);
echo json_encode($arrayData);
?>
There is another page that fetches theses results with JavaScript and displays it to the user.
The page takes more than 10 seconds to fetch the results and display them.
Does anyone have any idea on how to make it load faster? Is there a client based solution?
Thanks
Save all your data to file php file. For example if you want get array you can simply include a file and use data. Its simply way to using php faster.

Retrieve data from a link in php

I have a popup php page that contains this link within the php file (not the browser):
http://mydomain.com/member.php?id=75
how can I get the id value only to define another variables for the users on that page?
I used the $_SERVER['HTTP_REFERER']; to get the link where user came from.
many thanks,
You could just use $getId = explode("=", $_SERVER['HTTP_REFERRER']);
Then set $id = $getId[1] (Since the number is going to be the second position of the array).
Try this code here:
$query=parse_url($_SERVER['HTTP_REFERER'], PHP_URL_QUERY);
$REFERER_GET=array();
foreach(explode('&', $query) as $kv) {
list($key,$value)=explode('=', $kv);
$REFERER_GET[$key]=$value;
}
echo($REFERER_GET['id']);
id in your url is URL parameter which can be extracted using the $_GET variable.
just try printing $_GET variable. and choose the value of id as ($_GET["id"].

Calling images dynamicly using javascript and php

i am new to Javascript and i have created the code below it works fine no problem at all however i want to know what is i want to pull the image dynamically using php and javascript from mysql database how can i refactor my code bellow. thanks in advance for your contribution.
var myimage = document.getelementById("mainImage");
var imageArray =["images/overlook.jpg","images/garden.jpg","images/park.jpg"];
var imageIndex =0;
function changeimage(){
myimage.setAttribute("src",imageArray[imageIndex]);
imageIndex++;
if(imageIndex >= imageArray.length){
imageIndex = 0;
}
setInterval(changeimage, 5000);
One of several options.
Query the database for the column with the URL of the images.
$query = mysql_query("SELECT url FROM images");
Then something like this to get an array out of it:
$images = array();
while($row = mysql_fetch_array($query)){
$images[] = $row['url'];
}
Then generate this string (that you use in the Javascript provided):
var imageArray = ["images/overlook.jpg","images/garden.jpg","images/park.
using the array you retrieved from the database. You could use json_encode in PHP for this if you don't want to mess around with error prone string building.
$imagesAsJsonArray = json_encode($images);
Echo it. Done.
Not the most elegant of solutions. But it gives you something to play with. Check out a few PHP tutorials online and you'll soon get the hang of it.
Two choices:
Using PHP when your page is created, put an array of images in the page and use page-level javascript to cycle among them.
Using Ajax in the page, call from the page to the server to get the next image and then use client-side javascript to make that returned image visible on the page.

Returning a resultset from one page to another in PHP

I have 2 php pages: query.php and result.php.
In query.php, I am executing a query (select) statement. It's returning a resultset
$rs = mysql_query($query);
Now I want to return this resultset from query.php to another page result.php and work with it. Like this:
In query.php:
return $rs
and in result.php:
$result = executeQuery($query) // we get the resultset in this variable
while ($row == mysql_fetch_array($result){
//do something
}
If the above is not recommended, please provide me with alternatives. But I want the query function and resultset in different pages.
You could just include results.php in your query.php page if you're just looking to keep the code separate in the source files but aren't actually required to redirect from one page to another:
In query.php:
$rs = mysql_query($query);
include "results.php";
In results.php:
while ($row == mysql_fetch_array($rs){
//do something
}
As far as trying to "return $rs" from one page to another that's not how PHP works. The return statement is only valid within a function. If you want to actually pass data from one PHP page to another and will be redirecting to that other page then you'll need to use either a session, a cookie, pass it in the URL (i.e. use GET) or use curl and add it as a POST var.
If this is really the way it must be, store the result set in a database somewhere or in a file and give each result a unique name. Then pass that name to the next page so it can be retrieved.
query.php will redirect to result.php?result_set=ab24sdfsdfklls for instance.
This has the added advantage that you can use the result_set as often as you want. Visitors can have multiple result sets during one visit. They can share the URL of the result set page with other people, etc.
Just be sure to eventually prune the data store as it will just keep on growing, but that's another matter entirely.

Categories