jkkenzie Posted October 24, 2008 Share Posted October 24, 2008 $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. Quote Link to comment Share on other sites More sharing options...
jkkenzie Posted October 24, 2008 Author Share Posted October 24, 2008 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++; }); Quote Link to comment Share on other sites More sharing options...
dilum Posted October 24, 2008 Share Posted October 24, 2008 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; Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted October 24, 2008 Share Posted October 24, 2008 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. Quote Link to comment Share on other sites More sharing options...
jkkenzie Posted October 27, 2008 Author Share Posted October 27, 2008 Thanks alexweber15 this is the stuff i needed, actually i didn't go to school for this. You guys are my teachers. I owe you alot . It is warm and great to people like you here ,alexweber15, thanks. regards, jkkenzie Quote Link to comment Share on other sites More sharing options...
jkkenzie Posted October 27, 2008 Author Share Posted October 27, 2008 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 Quote Link to comment Share on other sites More sharing options...
jkkenzie Posted October 27, 2008 Author Share Posted October 27, 2008 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']);} Quote Link to comment Share on other sites More sharing options...
alexweber15 Posted October 27, 2008 Share Posted October 27, 2008 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! Quote Link to comment Share on other sites More sharing options...
jkkenzie Posted October 28, 2008 Author Share Posted October 28, 2008 Alright Bro! This is the place to be phpfreaks!! Thanks Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.