String variable array from url to meta tag SOLVED

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
peaforabrain
Forum Newbie
Posts: 7
Joined: Mon May 01, 2006 4:49 am

String variable array from url to meta tag SOLVED

Post by peaforabrain »

Hi,

I am stuck on an array issue.

The problem is, a variable that is passed from the url into the html document, will only appear as the word ARRAY. I know that this has to be converted using an array command, but as yet, I cannot find the correct command to process it.

this is the sort of url

http://www.*********.php?style[]=whatever

All that appears on the page is 'array'.

Could anyone throw a suggestion my way?

Thanks
Last edited by peaforabrain on Mon Oct 09, 2006 9:20 am, edited 1 time in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Where's that url built? And how?
peaforabrain
Forum Newbie
Posts: 7
Joined: Mon May 01, 2006 4:49 am

Post by peaforabrain »

Thanks for the interest.

The url can be generated from this search page;

http://www.beachwear.net/advanced_search.php?

and with a search generated from the options, it can be seen that the string variables are passed to the meta tags, some pass easily, but the style string doesn't.

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

sorry, a) I don't see where and how and b) I meant the php code anyway.

try

Code: Select all

<?php
$arr = array(1,2,3);

echo '?' . $arr . "<br />\n"; // ?Array
// because the array was used in a string context

echo '?' . join(',', $arr) . "<br />\n"; // array elements concatenated

foreach($arr as $e) {
	echo 'param[]=', $e, '&';
}
?>
maybe also of interest: http://de2.php.net/serialze
peaforabrain
Forum Newbie
Posts: 7
Joined: Mon May 01, 2006 4:49 am

Post by peaforabrain »

Thanks for the suggestion, but that didn't work, it outputs

param[]=Array


I have been through all the examples at php.net and still cannot get a value from the Array.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You will need to form your links differently. You could form the link by using a unique separator and exploding on it to get an array.

IE: http://www.somesite.com/page.php?param= ... -e-f-g-h-i

Code: Select all

$array = explode('-', $_GET['param']);

echo '<pre>';
print_r($array);
echo '</pre>';

//ouput

/*
Array
(
   0 => a
   1 => b
   2 => c
   3 => d
   4 => e
   5 => f
   6 => g
   7 => h
   8 => i
)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
chakhar86
Forum Commoner
Posts: 45
Joined: Mon Jun 05, 2006 1:36 am
Contact:

Post by chakhar86 »

try assign index to the param

Code: Select all

echo '$param[0] = '.$param[0];
or use vardump to know exactly what happen to that variable
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

peaforabrain wrote:Thanks for the suggestion, but that didn't work, it outputs

param[]=Array
Not with my code.
If you have used the

Code: Select all

foreach($arr as $e) {
        echo 'param[]=', $e, '&';
}
part and it's still displaying Array then your $arr is a multi-dimensional array <-> $e is an array, too.
But can't tell you for sure, I gave my crystal ball away for cleaning ;)
peaforabrain
Forum Newbie
Posts: 7
Joined: Mon May 01, 2006 4:49 am

Post by peaforabrain »

Thanks guys, but your suggestions do not work

As Volka as pointed out

it's still displaying Array then your $arr is a multi-dimensional array
I'm not too hot on these, so I will check out php.net to see if I can get any answers, unless you guys can give me a clue.

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

If you're using php5 http://de2.php.net/manual/en/function.h ... -query.php might help you.

Code: Select all

$arr = array(
		'abc'=>array('a','b','c'),
		'xyz'=>array('x','y','z')
	);

echo http_build_query($arr);
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

Ok, if I'm reading your initial post correctly, I think people are missing the problem here. It's not how to form the URLs, but how to access the values from form values that are arrays.

You have a page with a load of checkboxes with name="style[]". This is correct.

In the script listing_browse.php that receives the form submission you will have access to the following variable: $_GET['style']. This will be a single dimension array with numeric keys.

You haven't posted any code so I'm taking a guess here that you're doing something like:

Code: Select all

echo $_GET['style'];
This will output "Array", because that's exactly what it is, an array. To access single elements in the array you need to do something like:

Code: Select all

echo $_GET['style'][0];
to confirm this, as was suggested, try:

Code: Select all

var_dump($_GET['style']);
or

Code: Select all

print_r($_GET['style']);
These will show you the structure of your variable.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

choppsta wrote:Ok, if I'm reading your initial post correctly, I think people are missing the problem here.
:oops: sounds right
peaforabrain
Forum Newbie
Posts: 7
Joined: Mon May 01, 2006 4:49 am

Post by peaforabrain »

Chopsta, you have understood and solved my problem - almost. Thanks

I have the array outputted into a readable format, but,

how do you get rid of the brackets and such?

I am sure there is a simple explanation, but I havn't found it yet.
choppsta
Forum Contributor
Posts: 114
Joined: Thu Jul 03, 2003 11:11 am

Post by choppsta »

print_r and var_dump are just functions that can be used when debugging to quickly and easily see what the structure and the values of your variables are.

An array is a data structure that holds multiple values in a single variable. There is no set output style for this so your question has many answers, depending on how you want to output the data.

Let's say you want to output each of the styles seperated by a comma, you could do that simply with the following as was suggested by volka:

Code: Select all

echo join(', ', $_GET['style']);
Probably what you want to do is go over the array one element at a time using a loop and then you can customise the output how ever you want:

Code: Select all

foreach ($_GET['style'] as $key => $value)
{
	echo $key.': '.$value.'<br/>';
}
peaforabrain
Forum Newbie
Posts: 7
Joined: Mon May 01, 2006 4:49 am

Solved

Post by peaforabrain »

Thanks Chopsta, you are a great guy :D
Post Reply