Jump to content

[SOLVED] Arrays


jkkenzie

Recommended Posts

$result2 = mysql_query("SELECT*FROM tblcalc WHERE projectname='$project' order by Values_A ");
	$check=0;
        while($row2 = mysql_fetch_assoc($result2))
        {  
        $A[] = $row2['Values_A']; 
    $C[] = $row2['Values_C'];  
	$countryNM[]=$row2['country'];  
	$check++;         
        }

The above code gets data from database, the $check counts the number of records got, i want to put the results above in an array like this below:

$datay = array($A[0],$A[1],$A[2],$A[3],$A[4],$A[5],$A[6],$A[7],$A[8],$A[9],$A[10],$A[11]);
$datax = array($C[0],$C[1],$C[2],$C[3],$C[4],$C[5],$C[6],$C[7],$C[8],$C[9],$C[10],$C[11]); 

 

My problem is that my array gets from $A/$C [0] to $A/$C [11] i.e 0 to 11 or i can add manually even upto a higher value, i dont want to be adding manually, is there a way my array can get these values in the range equal to the $check.

 

I normally get errors when my array doesnt find values beyond the $check value, so i need to make my array more advanced to only get the $A and $C values that my query returned without including any quatation marks in between the array.

 

Thanks in advance.

Link to comment
Share on other sites

Is this a possibility? But its not working:

 

$result2 = mysql_query("SELECT*FROM tblcalc WHERE projectname='$project' order by Values_A ");
	$check=0;
        while($row2 = mysql_fetch_assoc($result2))
        {  
        $A[] = $row2['Values_A']; 
    $C[] = $row2['Values_C'];  
	$countryNM[]=$row2['country'];  
	$check++;         
        }

$controlC=0;
	$datay = "";
	while ($controlC <= $check)
	{
	           if ($controlC!=$check)
	             {
	              $datay = $datay .array($A[$controlC],);
	             
	             }else{
	               $datay =  $datay .array($A[$controlC]);
	             
	             }
	$controlC++;
	});


$controlC=0;
	$datax = "";
	while ($controlC <= $check)
	{
	           if ($controlC!=$check)
	             {
	              $datax = $datax.array($C[$controlC],);
	             
	             }else{
	               $datax =  $datax .array($C[$controlC]);
	             
	             }
	$controlC++;
	});

Link to comment
Share on other sites

I think you need create arrays like,

 

$datay = array($A[0],$A[1],$A[2],$A[3],$A[4],$A[5],$A[6],$A[7],$A[8],$A[9],$A[10],$A[11]);

$datax = array($C[0],$C[1],$C[2],$C[3],$C[4],$C[5],$C[6],$C[7],$C[8],$C[9],$C[10],$C[11]);

 

Anyway after complete your first while you already have  $A[0],$A[1],$A[2],.... and $C[0],$C[1],$C[2]....

 

So try like this after first while loop,

 

$datay = array();

$datex = array();

 

$datay = $A;

$datex = $C;

Link to comment
Share on other sites

you're both over-complicating this big time!!

 

jkkenzie, to make sure that the array's size is equal to check you can do one of 2 things:

 

- set the indices based on check:

$check=0;
// make sure both arrays are empty so the counting will match
$A = array();
$C = array();
while($row2 = mysql_fetch_assoc($result2)){ 
    $A[$check] = $row2['Values_A'];
    $C[$check] = $row2['Values_C']; 
    $countryNM[]=$row2['country']; 
    $check++;         
}

 

- set check based on the size of the arrays:

// make sure both arrays are empty so the counting will match
$A = array();
$C = array();
while($row2 = mysql_fetch_assoc($result2)){ 
    $A[] = $row2['Values_A'];
    $C[] = $row2['Values_C']; 
    $countryNM[]=$row2['country'];    
}
$check = count($A);
// if you want to be really strict you should cross reference this with the count($C)

 

but really use the first example (and its good practice to reset both arrays)

 

I normally get errors when my array doesnt find values beyond the $check value

 

this is easy: whenever you look for a value in the array just make sure the $index is smaller than the count of array:

 

function getArrayValue($index, $A){
    if(is_int($index) and $index < count($A)){
        return $A[$index];
    }
    return null;
}

 

PS - making sure the index is an int is actually good practice because bools for example or empty strings will evaluate to 0 when compared with the size of count but might behave unexpectedly when fetching a value from the array.

Link to comment
Share on other sites

Finally, How can i put the collected values for $A[] and $C[] to one variable like FROM:

$datay = array($A[0],$A[1],$A[2],$A[3],$A[4],$A[5],$A[6],$A[7],$A[8],$A[9],$A[10],$A[11]);

$datax = array($C[0],$C[1],$C[2],$C[3],$C[4],$C[5],$C[6],$C[7],$C[8],$C[9],$C[10],$C[11]);

 

 

TO THIS:

$datay = array($As);

$datax = array($Cs);

 

Because not all indices for the variables would go upto 11 or may even go beyong 11.

??

 

thanks again

Link to comment
Share on other sites

Actually i have solved the problem: For the sake of those googling, this is the answer: Getting data to arrays from database using php:

// Get data into arrays
$datay= array();
$result2 = mysql_query("SELECT * FROM tblcalc WHERE projectname='$project' ORDER BY Values_A ");
while($row2 = mysql_fetch_array($result2)) {array_push($datay, $row2['Values_A']);}

$datax= array();
$result2 = mysql_query("SELECT * FROM tblcalc WHERE projectname='$project' ORDER BY Values_A ");
while($row2 = mysql_fetch_array($result2)) {array_push($datax, $row2['Values_C']);}

Link to comment
Share on other sites

good stuff dude! :)

 

and pretty much everything is available on google as far as programming (or just about anything else) goes so its a great way to learn because if you have a question its unlikely you're the first person to have that same question ;)

 

but don't let that stop you from posting here!  :)

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.