mickeymouse 0 Posted December 18, 2018 Report Share Posted December 18, 2018 After a query which is iterative depending on a variable count (like $childcnt between 1 & 10), I place the data into an array $Cild[$childcnt] But then I can't access the data within each array, Is my code not correct? $Child[$childcnt] = mysql_fetch_row($Results); print("$Child[$childcnt[1]] - $Child[$childcnt[2]]"); Tks Quote Link to post Share on other sites
justsomeguy 1,135 Posted December 18, 2018 Report Share Posted December 18, 2018 Is there any other code? You're not changing the counter so it's going to keep overwriting the same element. You can also just push the results onto the end of the array without a counter. Quote Link to post Share on other sites
mickeymouse 0 Posted December 19, 2018 Author Report Share Posted December 19, 2018 Sorry I didn't want to bother you with all the code (now below) and sorry I didn't explain the problem properly. My problem is not with the logic but with the syntax. What happens is I get Parse error: syntax error, unexpected '[', expecting ']' If I don't have a solution to this, then I can try it as you suggest - just push results to the end. But I would prefer to have a solution to my syntax problem. Tks. $childcnt=1; $childdisp=11; $childtotal = 0; while($childcnt<10) {if($Row[$childdisp]<>"") {//================= GET CHILD ========================================= $query = "SELECT * FROM names WHERE `Ref`='$Row[$childdisp]' "; $Results = mysql_query($query, $link) or die ('Error = '. mysql_error()); $AffectedRows = mysql_affected_rows($link); if($AffectedRows < 1) {$Child[$childcnt[1]]=="$Row[$childdisp] Not in the Database."; $Child[$childcnt[2]]=="B";} else {$Child[$childcnt] = mysql_fetch_row($Results); print("$Child[$childcnt[1]] - $Child[$childcnt[2]]");} <-------SYNTAX PROBLEM???? $childcnt = $childcnt + 1; $childdisp = $childdisp + 1; $childtotal = $childtotal + 1;} else {$childcnt = 10;} } Quote Link to post Share on other sites
justsomeguy 1,135 Posted December 19, 2018 Report Share Posted December 19, 2018 Since you have that in double quotes, it's trying to replace the variable names with their values, but it sounds like it's getting confused. If you want to print those values then don't put them inside a quoted string, or use curly brackets to make everything explicit. http://php.net/manual/en/language.types.string.php#language.types.string.parsing I'm not sure what you're trying to print there, because $childcnt is a number but you're trying to access it like an array. Quote Link to post Share on other sites
mickeymouse 0 Posted December 19, 2018 Author Report Share Posted December 19, 2018 I must put double quotes or printing doesn't work. e.g. Print(ABC<br>) gives me error: unexpected < I am trying to print the 1st item in the array $Child[$childcnt] I am not trying to print $childcnt. $childcnt is indeed a number. It is the nth child and it is used to define which $Child array of the many I could have and as created by the earlier code: $Child[$childcnt] = mysql_fetch_row($Results); I expect to end up with Array $Child[1] containing an Array of child info followed by Array $Child[2] containing an Array of child 2 info, and so on. Then I'm trying to print the info from each array. Quote Link to post Share on other sites
mickeymouse 0 Posted December 19, 2018 Author Report Share Posted December 19, 2018 OK. I got around the problem with the following code. $Child = mysql_fetch_row($Results); $SavedName[$childcnt] = $Child[1]; $SavedSex[$childcnt] = $Child[2];} print("$SavedName[$childcnt], $SavedSex[$childcnt]<br>"); (The print actually occurs later on in my code as it has to fit into a form after some other printing). Many thanks for your help. Quote Link to post Share on other sites
justsomeguy 1,135 Posted December 19, 2018 Report Share Posted December 19, 2018 I must put double quotes or printing doesn't work. I don't know what you mean by that. print('The value of $Child[$childcnt] is ' . $Child[$childcnt] . ', and the value of $Child[$childcnt - 1] is ' . $Child[$childcnt - 1] . '<br>'); Quote Link to post Share on other sites
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.