/    Sign up×
Community /Pin to ProfileBookmark

Help with arrays and loops.

Heya, I’ve been using php for… about a year now, and I seem to have mastered all the area’s that I need, until now. It seems in a years time I have been able to do everything without the use of arrays! Anyways, I’m going to be working on a solution to the problem below, but would like to hear some of your solutions.

I have a database in which I have just one solitary box with this information:

[code=php]“00000Xaxei(01×01) says&#44; &#34;Hello.&#34;<br />” , “00000Bob(01×01) says&#44; &#34;Hiya.&#34;<br />”[/code]

and ect. What I am trying to do is make a chatbox in which each array element that starts with “000000” is displayed as is, and each “000001” should be displayed as is, but any other number should not be displayed.(e.g. 002001)

I was thinking using a loop to check each item in the array and display it if the number is 000000 or 000001 but if the number is anything else then it shouldn’t be displayed.

Thank you for all your help. I believe this can be done using an array and a loop… I just never use those so I’m not sure as yet how to do it. I appreciate any and all help with this situation.

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@JupacSep 02.2004 — Hey xaxei is this for da game?
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzauthorSep 02.2004 — Yes, I'm trying to figure out a way to do a secure php only private message system in the chatbox. I know Tatsimaou uses JS, and that's pathetic cause if u look at the code you can see all the Private Messages... even those not sent to you.(the code is hidden, but not well)
Copy linkTweet thisAlerts:
@JupacSep 02.2004 — Steal it from phpbb ? ...duno go on Aim
Copy linkTweet thisAlerts:
@NogDogSep 02.2004 — Untested:
[code=php]
# assumes data is in array $myArray[]
foreach($myArray as $value)
{
if(preg_match("/^0000[01]/",$value))
{
echo "$valuen";
}
}
[/code]
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzauthorSep 03.2004 — ok, could you explain to me exactly how that code works? (so I can learn from it)

like, the foreach and the [01]?
Copy linkTweet thisAlerts:
@MstrBobSep 03.2004 — [code=php]
# assumes data is in array $myArray[]
/* foreach is a type of loop that will
go through each element of an array.
Each value is stored in a specified variable,
in this case, $value. You could also do:
foreach($myArray as $key => $value)
and $key would be the key of each element
in the array. */
foreach($myArray as $value)
{
/*Okay, so now there's a regular
expression match. Regular expressions
are very powerful. This basically
says that if the variable begins
with 00000 or 000001 to echo it.*/
if(preg_match("/^0000[01]/",$value))
{
echo "$valuen";
}
}
[/code]


Regular Expressions Tutorial:

http://weblogtoolscollection.com/regex/regex.php
Copy linkTweet thisAlerts:
@Sux0rZh_jc0rzauthorSep 03.2004 — ok, now that i understand foreach, I'd like to dig a little deeper into pregmatch. how exactly would I modify it so that it checked for say.... 000021? or 000024? like, basically I will be running this foreach a lot, and the number will change quite frequently. would i do:

[code=php]$i = 000024 /*This number will be stored in a database, but for simplicity I will just have it as "$i = 000024".*/

if(preg_match("/^$i/",$value) OR preg_match("/^000000/",$value))
{
echo "$valuen";
} [/code]
? or would I have to arrange this another way?
Copy linkTweet thisAlerts:
@MstrBobSep 03.2004 — If you're number is going to be dynamic that way, then yes, your best bet is two different preg matches.
[code=php]
<?PHP
/* mysql connect. Mockup of grabbing number user's allowed to see */
$user_number = mysql_fetch_row($user);
$num = $user_number[0];
foreach($myArray as $value)
{
if(preg_match("/^$num/", $value) || preg_match("/^000000/",$value))
{
echo "$value<br>n";
}
}
?>
[/code]
×

Success!

Help @Sux0rZh_jc0rz spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...