Jump to content

[SOLVED] Using an Array...


Germaris

Recommended Posts

 

 

 

 

 

 

 

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

Link to comment
Share on other sites

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...

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

Uh yeah, you can always ask a question lol. :D

 

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 !!!

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!  :-)

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

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.