Page 1 of 1

String variable array from url to meta tag SOLVED

Posted: Sun Oct 08, 2006 1:40 pm
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

Posted: Sun Oct 08, 2006 1:44 pm
by volka
Where's that url built? And how?

Posted: Sun Oct 08, 2006 3:56 pm
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

Posted: Sun Oct 08, 2006 4:02 pm
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

Posted: Mon Oct 09, 2006 3:13 am
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.

Posted: Mon Oct 09, 2006 3:19 am
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
)

Posted: Mon Oct 09, 2006 3:24 am
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

Posted: Mon Oct 09, 2006 4:46 am
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 ;)

Posted: Mon Oct 09, 2006 6:24 am
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

Posted: Mon Oct 09, 2006 6:37 am
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);

Posted: Mon Oct 09, 2006 6:51 am
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.

Posted: Mon Oct 09, 2006 6:58 am
by volka
choppsta wrote:Ok, if I'm reading your initial post correctly, I think people are missing the problem here.
:oops: sounds right

Posted: Mon Oct 09, 2006 7:34 am
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.

Posted: Mon Oct 09, 2006 8:22 am
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/>';
}

Solved

Posted: Mon Oct 09, 2006 9:20 am
by peaforabrain
Thanks Chopsta, you are a great guy :D