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
https://forums.phpfreaks.com/topic/233739-help-with-displaying-double-explode/
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"

?>

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.