Jump to content


Photo

A little help needed with a array problem.


  • Please log in to reply
5 replies to this topic

#1 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 09 June 2006 - 07:47 PM

I am making a script to send out a email when there are events happening on the current day. At the moment it sends out one email for every event, this becomes a problem when there are three or four events on one day [img src=\"style_emoticons/[#EMO_DIR#]/huh.gif\" style=\"vertical-align:middle\" emoid=\":huh:\" border=\"0\" alt=\"huh.gif\" /]

I get the events from the db using this at the moment
while ( $row = mysql_fetch_array($result ) ) {

$event_type = $row[event_type];

}

The problem with this it over writes the varables each time. I could use

while ( $row = mysql_fetch_array($result ) ) {

$i = $i+1;

$event_type$i = $row[event_type];


}

But then I would end up with lots $event_type$i But i would'nt know how many [img src=\"style_emoticons/[#EMO_DIR#]/excl.gif\" style=\"vertical-align:middle\" emoid=\":excl:\" border=\"0\" alt=\"excl.gif\" /]

Is there some way of putting them into an array, then I could count how many were in the array, and use something like this.

    

$cnt = count(array_name);            
for($i=1; $i<$cnt; $i++){

CODE
            

I would like the end product to be something like

$message = "The following events are happening to day</ br>
$event_type1</ br>
$event_type2 </ br>
etc etc


Thanks in advance James

#2 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 09 June 2006 - 07:53 PM

$events = array();
while ( $row = mysql_fetch_array($result ) ) {

$events[] = $row[event_type];

}

[table]



Don't worry, the printer fairies will sort it out.

#3 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 June 2006 - 07:53 PM

while ( $row = mysql_fetch_array($result ) ) {
$event_type[] = $row[event_type];
}
then when you are building your email, you would add a while loop to your $message

$message = "The following events are happening today:";
$x = 0;
while ($event_type[$x]) {
   $message .= $event_type . "<br>";
   $x++;
}

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#4 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 09 June 2006 - 07:54 PM

Something like

$event_type = array();

while ( $row = mysql_fetch_array($result ) ) {

         $event_type[] = $row[event_type];
}

echo 'The following event types happened today<br>' . join ('<br>', $event_type);


If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 09 June 2006 - 08:00 PM

dammit Barand I forgot about join again [img src=\"style_emoticons/[#EMO_DIR#]/laugh.gif\" style=\"vertical-align:middle\" emoid=\":laugh:\" border=\"0\" alt=\"laugh.gif\" /]

yeah you can use join or even implode on $event_type, after the first while loop, instead of the 2nd while loop.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 smith.james0

smith.james0
  • Members
  • PipPipPip
  • Advanced Member
  • 122 posts

Posted 09 June 2006 - 08:52 PM

Thanks for the quick replys, I will try them out tomorrow.

Thanks James




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users