Jump to content

help with displaying double explode


Jnerocorp

Recommended Posts

Hello,

 

I am just trying to learn new stuff about php so I was trying to rebuild the function that sets $_GET['name'].

 

I got it to work with only one "?name=value"

 

but im having trouble setting the multi name=value&name2=value2

 

What I want it to do is the same that I have done with a single variable

 

Here is the code I have so far:

 

<?php
# Creating the get function

get();

echo $_GET['username'];

function get()
{
	$queries = $_SERVER['QUERY_STRING'];
	$check_for_multiples = strpos($queries, "&");
	$t1 = substr_count($text, '&');
if($t1 > 0) { $total_variables = add($t1, 1); } else { $total_variables = $t1; }
	$del1 = "&";
	$del2 = "=";


if($check_for_multiples > 0) 
{
	$array1 = explode("$del1", $queries);
foreach($array1 as $key=>$value)
{
	$array2 = explode("$del2", $value);
foreach($array2 as $key2=>$value2)
{
	$array3[] = $value2; 
}
}
	$afinal = array();
for ( $i = 0; $i <= count($array3); $i += 2) 
{
    if($array3[$i]!="")
{
	$afinal[trim($array3[$i])] = trim($array3[$i+1]);
	echo $afinal[$i];
    }
}

}

if($check_for_multiples <= 0)
{

	$queries = explode("=", $queries);
	#echo "VAR=".$queries[0]." VALUE=".$queries[1]."";
	$_GET[''.$queries[0].''] = $queries[1];

}

}



function add($a, $b)
{
$answer = $a + $b;
	return $answer;
}

function subtract($a, $b)
{
$answer = $a - $b;
	return $answer;
}

function multiply($a, $b)
{
$answer = $a * $b;
	return $answer;
}

function divide($a, $b)
{
$answer = $a / $b;
	return $answer;
}


 

Link to comment
Share on other sites

i'm confused what you're trying to do. 

 

if you have the query string in your url:  ?name1=value1&name2=value2

 

then in your php:

<?php

echo $_GET['name1']; // will output "value1"
echo $_GET['name2']; // will output "value2"

?>

Link to comment
Share on other sites

Or, you could just put multiple names, separated by a comma or forward slash, or whatever you please then split them with explode.

 

 

index.php?name=Max,John,Mr. Potato Head

<?php
$names = explode(',', $_GET['name']);

 

This would leave you with:

 

array([0]=>Max [1]=>John [2]=>Mr. Potato Head)

 

Easy to access it by using a foreach loop, or even just a for loop using the count of the array to help you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.