Jump to content

help echoing exploded array .... PLEASE Help!!


Recommended Posts

thanks, for your help ...

 

but i am no getting array prented on the screen...

my source looks like this like this


// explode the male  name so we can see if its het for anything
$atribute = explode("/",$male);
// this should give me exp. (co) (pastel) (albino) (piedball)
$het = array();
foreach($atribute as $key=>$atribute){
	if($key == 1){
		// this sotres the snakes genetic background
		$gene_type = "$atribute";

		}
	if($key == 2){
		// this sotres the snakes base morph
		$morph = "$atribute";	
	}
	if($key >2){
	  	$het[] =  "$atribute";

	}
}
$numb_hets = count($het);	

// ooooooooooooooooooooooow!!!!!
if($numb_hets == 0){		
		// co / dom / rec 
	     $gene_type;
		//  base morph
		 $morph;
		/// not het 
		$hets = "no";
		// check what they Gentic back ground is....
		if($gene_type == "co"){	
			$what = "Codominant";
		}elseif($gene_type == "dom"){
			$what = "Dominant";	
		}elseif($gene_type == "rec"){
			$what = "Reccesive";
		}elseif($gene_type == "wild"){
			$what = "Wild Type ";	
		}
	echo "
	<table><tr><td><u>Background</u></td><td><u>Morph</u></td><td><u>Hets.....</u></td></tr>
	       <tr><td><u>$what</u></td><td><u>$morph</u></td><td><u><pre>";
		   
		   print_r($het);
		   echo "</pre></u></td></tr>
	</table>
	";	
	// end of function     
}

Link to comment
https://forums.phpfreaks.com/topic/239815-help-echoing-exploded-array-please-help/
Share on other sites

okay my data comes from a form

if(!isset($_POST['male'])){ $errors[] = "You Must Select A male to breed!" ;}
//check if a female was speciefied
if(!isset($_POST['female'])){ $errors[] = "You Must Select A female to breed!" ;}
$male = $_POST['MALE'];
$female = $_POST['feMALE'];


// explode the male  name so we can see if its het for anything
$atribute = explode("/",$male);
// this should give me exp. (co) (pastel) (albino) (piedball)
$het = array();
foreach($atribute as $key=>$atribute){
	if($key == 1){
		// this stores the snakes genetic background
		$gene_type = "$atribute";

		}
	if($key == 2){
		// this stores the snakes base morph
		$morph = "$atribute";	
	}
	if($key >2){
// this is where i need help 
// the snake could have anywhere from 1-5 het traits
i need for $het[] = $atribute to be stored into single varbles that i can use to continue my code for my breed function .................
	  	($het[] =  "$atribute" )  can not be echo and get each value .... all i get is array....

	}
}

 

basiclly its just like that site pythonkings.nl

but i dnt have a breed function yet im stuck ........

and thats whats stopping my launch for the site......

There is a complimentary function to explode called implode(), which does basically the opposite of explode. You give it an array, and a delimiter, and you get back a string. For example

$array = array("attribute1", "attribute2", "attribute3");//example output that would be in your $het array
//lets say we wanted our string to be like this: attribute1, attribute2, attribute3
//we could use ", " as our delimiter
$string = implode(", ", $array);//Output: attribute1, attribute2, attribute3
//we could also use" - " if we wanted
$string = implode(" - ", $array);//Output: attribute1 - attribute2 - attribute3

 

You can use anythign as a delimiter. If you wanted to have them each on a separate line you could use <br />. If you wanted something more complicated, like as list items (<li>'s) then let me know, as its slightly more complicated, and I'll give you a hand

 

 

Implode: http://php.net/manual/en/function.implode.php

 

Hope this helps

thanks buddy glad to see i could get some real help!!!!

okay i will try the implode function .

but i was trying to get them into varibles so i could test them against the game like forinstance

if u have a clue about how ball pythons breeding work then u would understand like example:

male would eqaul :

Co/pastel/albino/piedball

where (co) equals the base morphs background

and the 2 trait will equal the base morph (pastel)

and evrything after comes from the row of the datbase what snake is het for (albino) / (piedball):

problem is i wont kno what all the snake is het for?????

so i made it so a snake can only be het for 5 traits......

witch mean the array would look something like this

// $MALE IS WHATEVER THE SNAKES ATRIBUTES ARE : like Co/pastel/albino/piedball
$MALE = $_POST['MALE'] 
// so i
$at =  explode ("/"$male);
//and did this:
// wrote an array for counting
$stat = array;
foreach($at as $key=>$att){
// i want to write a varible for each stat like
$stat[] = $att;

}
but u cant echo $stat u would just get array and i kno i could just echo in the loop but im not trying to echo i just want to kno if $stat will hold each trait like this:
$stat[] = "co";
$stat[] = "pastel";
$stat[] = "albino";
$stat[] = "piedball";
///?????? but obvisuly u cant do that but i wanna kno how u go about doing that cause evan after implode u still dnt get what im looking for ???? u digg? 

1 - your code says if het_count == 0 then print stuff out in a table 

Should that be if hetCount != 0 then print stuff?

and then put in a catch to say there were no results if the count==0

 

If not then

 

Can you do some debugging for me please.

Change your code slightly to match the following. Get rid of all code following the $atribute = explode("/",$male); line, add in the debug echos and lets see what the result is. am wondering if the $atribute is an array and also what delimeter shows up in $male

 

If we can establish that you do have an array and a correct $male text then we can take the next step.

 

Shane

 

......
$male = $_POST['MALE'];
$female = $_POST['feMALE'];

echo "Male=".$male;


// explode the male  name so we can see if its het for anything
$atribute = explode("/",$male);

$myCount=count($atribute);

echo "Count =".$myCount;

 

DIDNT UNDERSTAND that one but i can be more specific i guess... :confused:

 

example:

<?php
// $_POST['MALE'] would look like this with the test snake im using:
$male = $_POST['MALE'] = "/co/pastel/albino/piedball";
// i added those slashes (/) to use as an explode delimiter so if u explode u get this
$ats = explode("/",$male);
// after explosoin (Kaboom!!!!!) Now Its an array  (co)  (pastel)  (albino)  (piedball)
// foreach statment comes into play 2 ad a controller varible (hope i used that statment right lol! "control varible") 
foreach($ats as $key=>$ats){
// now the first to are easy i can just write an if statment for those guys  cause 
if($key == 1){
  // have to say exacly equal to  zero cause foreach  stores a blank value with the zero key(dnt kno why ) and beacuse im in a loop it would go to the next if i would have used(if($key>0)) 
// anyway the first key key 1  is a representation of the genetic background exp. (co) or short for coDominant  
// so i stor that into a varible
$gb = $ats;
// now the second  if statment says the following because again i just need the next varible (pastel) which is the base color/pattern mutation( im not loosing u am i ? )
if($key == 2){
$base = $ats;
}
// now her is where that php heffer i usta love gets tricky lol!
// the following $ats varibles are considered hets as to thats how they were posted on my database and looped into a select list...
// so heres wherre i hit the way  i dnt kno how many traits the snake is het for i want to store all the following traits into varibles that the function can comunicate with something like this: (if this is allowed)... 
if($key >2 ){
$hets = array();
$hets[] = $ats;
// but that doesnt work !!!!!  :'(
// now i kno a vetran coder would say implode or list but u must kno whats in the array to list and implode will just put whats left of the array backinto one which i dnt need so to ask the question again ....
[b]how do i store the rest of that exploded array into varlbes so i can use them in my next function? [/b]
}
}
?>

lol, is that clear enough , thanks for your help buddie!!

 

PHP IS LIKE A MEAN BITCH THAT NEVER Aplogize when she does wrong!

The first item in the array is empty because there's a leading slash in your string:

 

"/co/pastel/albino/piedball"

 

The delimiter is what delimits two strings from each other, so the left-side of the first slash is treated as a string (even though it's empty). Remove the leading slash to remove the empty 0 index.

 

A cleaner solution for conditionally setting the variables here would be a switch statement:

 

switch ($key)
{
    case 0:
        $gb = $ats;
        break;
    case 1:
        $base = $ats;
        break;
    default:
        $hets[] = $ats;
}

 

The reason your code wasn't storing the remaining values in the array before, is because you were redefining it as an empty array in each iteration:

 

$hets = array();
$hets[] = $ats;

 

That would mean only the last item in the array is stored. Move the array declaration to outside (before) the for-each loop and you'll have better success.

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.