Germaris Posted August 31, 2007 Share Posted August 31, 2007 Hi there! I perfom a simple query for retrieving phone numbers: $query = mysql_query( "SELECT * FROM $table WHERE phone='$sphone') or die ( "select error " . mysql_error() ); if ( mysql_num_rows( $query ) > 0 ) while ( $row = mysql_fetch_array( $query ) ) { $mystring .= "<font color='#000099'>".$row ["lastName" ]." "."</font>"."<font color='#663399'>".$row ["firstName" ]."</font>".", "."<font color='#0033CC'>".$row ["school" ]." - ".$row ["year" ]."</font>"."<br />"; } print $mystring; (I send $mystring to a .swf file where it is parsed, this is the reason why I use FONT tags which are amongst the few Flash can accept. But this has no importance for solving my problem...) When only one number is found: no problem But if there's duplicates, it becomes more complicated (for me!) to handle... How can I get all the results (in an array or each designated by a new string ?) so I can choose, later in the script, which one to display (formatted as shown in the code) depending on some conditional logics? If I sort within an Array, how do I create it and HOW DO I EXTRACT DATA FROM IT? This is the most important which I don't know !!! Many thanks in advance! Regards. Gerard Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 You just want an array with multiple strings? Change "$mystring .=" to "$mystring[] .=" and it will be an array of strings. Quote Link to comment Share on other sites More sharing options...
Germaris Posted August 31, 2007 Author Share Posted August 31, 2007 Thanks for your interest and replying so fast! Yes, I did this BUT AFTER, how can I extract the data from this array ? Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 foreach ($mystring as $eachstring) \\this code will be executed for every string in $mystring[] Quote Link to comment Share on other sites More sharing options...
Germaris Posted August 31, 2007 Author Share Posted August 31, 2007 OK. I did what you say. It works fine. BUT: In my test, I knew in advance what the result would be. And in this test we obtain three rows from the table. Each one containing lastName, firstName, school and year. print $mystring; just give me the third row (the last one) How can I dot to get the other two (the first and the second) or only one of those two? I believed that arrays in PHP (just like in Flash ActionScript) have indexed strings ([0], [1], [2], ... )and that we could call for any of them to print... Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 foreach ($mystring as $eachstring) print $eachstring; Is that what you are asking? Quote Link to comment Share on other sites More sharing options...
Germaris Posted August 31, 2007 Author Share Posted August 31, 2007 No. I may want it in the future but it isn't what I want for the moment. What you wrote is working and is exactly what I did. And it gives me only the third string (the last) of the array. What should I write if I want, for example, the second string? Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 mystring[1]; Quote Link to comment Share on other sites More sharing options...
Germaris Posted August 31, 2007 Author Share Posted August 31, 2007 GOOD! I wrote: print $mystring[1].$mystring[2]; And it worked fine. Congratulations! Now I've understood the "mechanics". I f you have some time, may I ask you something else? If not, I can wait... Anyway, thank you very much for all you've done so far! Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 Uh yeah, you can always ask a question lol. Quote Link to comment Share on other sites More sharing options...
Germaris Posted August 31, 2007 Author Share Posted August 31, 2007 Uh yeah, you can always ask a question lol. Okay. Thanks! I go for it: 1 - How can I automate the printing of the strings (print $mystring[1].$mystring[2]) in relation with the number of results the array contains? May be I must include in my query a COUNT statement which will return the number of rows containing the searched phone number? So, after, I can use this result to say to the machine (!!!) for example: print 4 strings ($mystring[1].$mystring[2].$mystring[3].$mystring[4]); I know, this seems a little akward but I hope you'll understand! :-) 2 - How can I "explode" a chosen string in the array so I can send it as separate elements to my Flash file? Example: I want to "explode" mystring[0] so I can send this to Flash: $new_string = explode(mystring[0]); $new_string .= "&a=".row['lastName']."&b=".row['firstName']."&c=".row['school']."&d=".row['year']; print $new_string; I know for sure what I wrote here is wrong, but it the idea that count! lol !!! Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 1. foreach ($mystring as $eachstring) print $eachstring; Will print all of the strings in the array. Is that not what you are asking? If you want to do something relative to the number of elements, you can use count($mystring). That will return the number of elements in the array. 2. You can't return the fields again after you have converted it to a string unless you delimited it in your string. You might be able to catch the font tags using regular expressions, but that is probably to complicated for what you are trying to do. You could change the way you do it to make the $mystring array into a two dimensional array that will contain all the fields, then you can convert it to the string with the font tags when you send it to flash. Quote Link to comment Share on other sites More sharing options...
Germaris Posted August 31, 2007 Author Share Posted August 31, 2007 1 - foreach ($mystring as $eachstring) print $eachstring; Will print all of the strings in the array. WRONG. IT PRINTS ONLY THE LAST STRING.(see one of my posts). I DON'T KNOW WHY... Is that not what you are asking? NO If you want to do something relative to the number of elements, you can use count($mystring). That will return the number of elements in the array. THIS IS WHAT I WANT. BUT WHERE DO I USE count($mystring)? 2 - font tags using regular expressions IN THAT PART, I DON'T USE FONT TAGS (see the code I wrote). I WISH TO SEND VARIABLES VALUES ("&a=".row['lastName'] for example). Font and color of text are assigned by the Flash Fields which receive the variables. to make the $mystring array into a two dimensional array that will contain all the fields HOW CAN I DO THIS? THIS IS VERY INTERESTING AND I THINK WILL BE USEFUL FOR ME !!! Thanks for your help! :-) Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 1. To print all the entries, you can do this: <?php $query = mysql_query("SELECT * FROM $table WHERE phone='$sphone'") or die ( "select error " . mysql_error() ); // GET ALL THE ENTIRES $max = mysql_num_rows($query) or die(mysql_error()); while ($row = mysql_fetch_assoc($query)) { $mystring[] .= "<font color='#000099'>".$row ["lastName" ]." "."</font>"."<font color='#663399'>".$row ["firstName" ]."</font>".", "."<font color='#0033CC'>".$row ["school" ]." - ".$row ["year" ]."</font>"."<br />"; } for ($m=0; $m<$max; $m++){ echo $mystring[$m]; } 2. How do you want to explode it? Quote Link to comment Share on other sites More sharing options...
Germaris Posted August 31, 2007 Author Share Posted August 31, 2007 1 - This code is CRYSTAL CLEAR !!! Thank you! 2 - I want to explode $mystring[0] to be able to send it to Flash as a collection (suite) of variables as described in one of my previous postings: <?php $new_string .= "&a=".row['lastName']."&b=".row['firstName']."&c=".row['school']."&d=".row['year']; print $new_string; ?> Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted August 31, 2007 Share Posted August 31, 2007 But how do you want to explode it? Do you want to get each $row[] separately and then combine it with the newstring? Quote Link to comment Share on other sites More sharing options...
lemmin Posted August 31, 2007 Share Posted August 31, 2007 I just typed this out and tested it, just to make sure and it prints "testtesttest," which is the three strings in the array as you can see: $i=3; while ($i>0) { $mystring[] .= "test"; $i--; } foreach($mystring as $eachstring) echo $eachstring; It is the same code you are using, but without the query and variables. You must be doing something wrong for it to only print the last element.Maybe you should post the current code you are using for it. Did you make sure to change $mystring .= "<font color... to: $mystring[] .= "<font color... ? $mystring .= "<font color... I want to "explode" mystring[0] IN [mystring], I DON'T USE FONT TAGS You definately confused me with that. Quote Link to comment Share on other sites More sharing options...
sasa Posted August 31, 2007 Share Posted August 31, 2007 create $new_string when you create $mystrimg [while ($row = mysql_fetch_assoc($query)) { $mystring[] = "<font color='#000099'>".$row ["lastName" ]." "."</font>"."<font color='#663399'>".$row ["firstName" ]."</font>".", "."<font color='#0033CC'>".$row ["school" ]." - ".$row ["year" ]."</font>"."<br />"; $new_strings[] = "&a=".row['lastName']."&b=".row['firstName']."&c=".row['school']."&d=".row['year']; } and foreach($new_strings as $new_string) print $new_string;) Quote Link to comment Share on other sites More sharing options...
Barand Posted August 31, 2007 Share Posted August 31, 2007 1 - foreach ($mystring as $eachstring) print $eachstring; Will print all of the strings in the array. WRONG. IT PRINTS ONLY THE LAST STRING.(see one of my posts). I DON'T KNOW WHY... Have you put a ";" at the end of the first line? Quote Link to comment Share on other sites More sharing options...
Germaris Posted September 1, 2007 Author Share Posted September 1, 2007 Have you put a ";" at the end of the first line? Yes. And here is the BIG mistake !!! Mea culpa. Thanks! Quote Link to comment Share on other sites More sharing options...
Germaris Posted September 1, 2007 Author Share Posted September 1, 2007 But how do you want to explode it? Do you want to get each $row[] separately and then combine it with the newstring? Not at all. I only want to explode $mystring[0] Purpose: to display each of the fourth element (lastName, firstName, school, year) in separate fields in my Flash file. Quote Link to comment Share on other sites More sharing options...
Germaris Posted September 1, 2007 Author Share Posted September 1, 2007 create $new_string when you create $mystrimg [while ($row = mysql_fetch_assoc($query)) { $mystring[] = "<font color='#000099'>".$row ["lastName" ]." "."</font>"."<font color='#663399'>".$row ["firstName" ]."</font>".", "."<font color='#0033CC'>".$row ["school" ]." - ".$row ["year" ]."</font>"."<br />"; $new_strings[] = "&a=".row['lastName']."&b=".row['firstName']."&c=".row['school']."&d=".row['year']; } and foreach($new_strings as $new_string) print $new_string;) sasa, I think you got it !!! You understood the problem and gave THE solution. Many thanks. Quote Link to comment Share on other sites More sharing options...
Germaris Posted September 1, 2007 Author Share Posted September 1, 2007 You definately confused me with that. I understand why... But read my replies to the other friends (sasa code in particular) and I think you'll understand what I was looking for... Anyway, I thank you very much for your advices and the way you handled the problem! Quote Link to comment Share on other sites More sharing options...
Germaris Posted September 1, 2007 Author Share Posted September 1, 2007 I consider this topic as solved. I'm very satisfied with the results I get now in my Flash file. This wouldn't have been possible without the help you provided to me. I want to deeply thank all the "team" !!! :-) Best regards, Gerard 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.