Tracking Forums, Newsgroups, Maling Lists
Home Scripts Tutorials Tracker Forums
 
  HOME    TRACKER    PHP




For Loop Q


I have a "for loop". In this "for loop" a variable called $reg is set.
Each iteration of the "for loop", $reg is assigned to a new value.
My question is: how I can store the values of $reg each iteration in an array?

Below is some pseudocode, which I hope further illustrates what I mean.

$count = 60;
for ($i = 1; $i <= $count; ++$i) {
$reg =
}

This has got me a bit stumped.

After I have managed to make the array.
I would then like to explode this array out into variables - each one assigned a value of $reg

I realise that this may be quite a basic Q. And i really do apoligise. I have read up on arrays and am having trouble applying it to this
situation. Thanks very much.

P.S A further complication that I would like to accommodate is that $reg may be assigned a number of values in a single "for loop". I would like to
collect all these values.
The pseudocode for this:

$count = 60;
for ($i = 1; $i <= $count; ++$i) {
$reg =
$reg =
}

If this post is difficult to follow - dont hesitate to post back and I will try my best to expand. It is just I didnt want to make this post too long and
put people off.

P.S It may be helpful to know what is actually in the "for loop":

for() {

$regex = "/(([A-Za-z0-9]+_+)?[A-Za-z0-9]+-+)?[A-Za-z0-9]+.+)?[A-Z
a-z0-9]+++))*[A-Za-z0-9]+@((w+-+)?w+.))*w{1,63}.[a-z
A-Z]{2,6}/";
preg_match_all($regex, $data, $reg))

}




View Complete Forum Thread with Replies

See Related Forum Messages: Follow the Links Below to View Complete Thread
If Loop In A While Loop Producing Incorrect Results
I have this code that checks to see what the value of "del" is and places a certain word depending on if its a 0 or 1. PHP Code:

Creating Table Rows For Ever Other Loop In A WHILE Loop
I'm trying to create a table using a MySQL fetch array while loop that would look like this:

CONTENT1     CONTENT2
CONTENT3     CONTENT4
CONTENT5     CONTENT6

I need a <TR></TR> for every other piece of content. But I don't know how to do this with a loop that uses a MySQL fetch array

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

{
PHP CODE THAT IS LOOPED;};

I'm trying to remember from my old college Java class that we did something with loops for whenever the counter variable was even the loop would do something special... I don't know if this is possible with a while+mysql_fetch_array loop though.

Creating A For Loop Inside Another For Loop.
I'm trying to write a script that will display questions that are in one flat file and answers from another flat file. In addition, there are copies of these files with their own data in multiple folders.

I created a for loop to work with an array of the different file paths to the different folders. Then I created a for loop inside of that one to display the questions and answers files that is in each folder. All I get when I run the script is the first folder and it's questions and then it stops there. PHP Code:

Get A Varable To Loop Out Side The Loop
i need to get the varable to loop out side the loop but the condition set within the loop so the varable loop's outside. Code:

For Loop And While Loop. Which One Is Faster?
For loop and while loop. which one is faster?
I see many articles fighting over it and different people come up with
different results.

For Loop In PHP
I am trying to print a HTML table using PHP (with a countdown counter) but I do not see the table being display. Can someone highlight the error i m making ...

<?php
$input=8;
print $input;
print "<table border=3 bordercolor=0084A5>";
for ($cnt=$input+1 ; $cnt = 1 ; $cnt=$cnt-1)
{
print "<tr>";
print "<td>$cnt</td>";
print "</tr>";
}
print "</table>";
?>

Help In Loop For
Suppose I have this:

for($i = 0; $i < count($data); $i++){
$blocked = isBlockedForMe($my_id, $account_id); // true or false
}

I want when the $blocked is true to SKIP to the next step, I dont want
to BREAK, I want something like this:

for($i = 0; $i < count($data); $i++){
$blocked = isBlockedForMe($my_id, $account_id); // true or false
if($blocked){
....SOMETHING HERE TO SKIP but continue in the loop
}
// I dont want to show/execute what is below
............
............
}

If/Then In A While Loop
I tried a bunch of different things, but am I able to do an if/then within a while loop? Here is the code that I am basically trying to do... PHP Code:

While Loop...
I've been working on a relatively simple function, that just won't work. Here it is:
PHP Code:

For Loop Or While Loop
I was wondering if this could be achieved with a for loop or a while loop basically returning the time for up to 20 years: PHP Code:

While Loop
I have a while loop thats get comments on news articles from a database. I am using a class as my database connection method. It only gets the first record. Here is my code:

$i++ Loop
What I'm trying to do is echo a error message if a field(s) is left blank without having to do alot of if statements. The code I have now works but will only show one number
at a time. PHP Code:

For Loop?
I'm trying to limit the number of cells in a table to 4 across

echo "<table border=1>";
echo "<tr>";
while($result = mysql_fetch_array($sql)){
echo "<td><IMG SRC="$result[photo]""></td>";}
echo "</tr>";
echo "</table>";

so if there are 15 entries in the db, it gives me 15 cells in one row. How can i limit the cells in a row to 4, and have it generate however many rows to display all. I've been experimenting with for loops, but no luck.

Loop Through JS Using Php?
I am trying to loop through a JS script as below using php to get the contents for a drop down list but I cant seem to get the loop to work. Here's what I have been trying..

// Heres the JS for the drop down list. I cant have this static as it is here as there could be 10-20 results in the drop down list so I need to loop through my DB result and loop the below script also Code:

LOOP IN A LOOP
I am making a navigation system for a website and I want all the links to be in a table on a database so that the links can easily be added to or subtracted from when found necessary. The way the navigation works is that it is a horizontal navbar with drop down menus filled with sub categories Code:

Using A While Loop In A For Loop - Can This Be Done?
can you use a while loop inside a for loop; without contradicting or excluding your for loop? I would test it myself; except my server is having Server Error 500 issues right now. so I am not able to do a trial and error test at this time.

something like this:

for ($x = $skip2;$x<=$pagedisplay;$x++){
while ($myfield != $maxed_out) {
  echo " <a href="". $_SERVER['PHP_SELF'] ."?skip=$x">". ($x+1) ."</a> ";
 }
}

For Loop
I work mostly with flash but use php to talk to databases so I am not strong in php. I am passing some variables from flash that I want to add to a database. They are in the form of sid0=A&sid1=B&sid2=C and so on, n is the total number of variables passed. I believe the server I am working on uses PHP 4.1. Code:

Loop
How can I make a loop for this formula?

P (1 + r/365)365

Loop Only Gets 1 Value!
And it kicks out the results on the page in hyperlinks, all the zipid's are 14, they should be looping through and getting the search_id that matches the zipcode. Code:

While Loop And Div
I have some code which loops whilst there are some results in the database.  These are displayed inside a while loop. However any div inside the loop does not display. Code:

While Loop
I´m trying to make an application to insert new data into a table. So i extract the fields of the table and let the user introduce new values. The problem is when i´m trying to put them into a table.

Firstly i get the new data sent via form with a while loop, because the number anda name of the fields can be different in each case. I do it like this

$sql = " SHOW COLUMNS FROM " .$_SESSION["nomtab"]. " ";

$res= mysql_query($sql);

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

$$row[0] = $_POST["$row[0]"]; }

Now i introduce correctly the first´s field value and i create two auxiliar variables $primercampo and $valorprimercampo which contains the name of the first field and its value, so i can use them later to update the values, it goes like that Code:

Loop
I'm trying to add the following link to my display page. 

<a href="applyforstake.php?item_id=1>Apply"

This is the code taken directly from the display page:

echo "<td>"."<a href="applyforstake.php?item_id=1">Apply"."</td>";

This code works well for displaying the link in my display page, however, I want the item_id to be equal to the auto_increment column on each row.  In other words, for the first row on the display page, the item_id will be 1, the second row item_id should be 2 and so on for however many rows are in the table. How do I accomplish this.

For Loop And Arrays
I'm trying to create a function to execute multiple backups at once, using the following code:

Pause Between Each Loop
Is there a simple way to pause for a certain time after each loop? Thanks in advance.

While Loop Timing Out
Ok I just ran into this problem. I have a while statment going and the server will time it out because it going to long does anyone know a way around this?

I found somewhere online that says that PHP times out a while loop after a certan amount of time even if you have set_time_limit() or set it in the php.ini file.

For somethings I can just make it break after a number of loops and start back where it was but can't do it on some things.

How To Loop Query
I want to display all the values from certain row, how do i query and use loop so it will display all the value?

Horizontal Loop
I'm building a php form that retrieves about 25 categories from a MySQL database and creates a list of check boxes so users can select multiple categories before submitting the form. Right now I'm using the code below and it creates a vertical list of categories with check boxes.

For Loop Performance
Is a difference in performance between the two following for loops?

Txt File Loop
How would I go about making a loop to open a .txt file, read the first line, then do something with it and them loop to the next line.

For Loop With Txt Files
I want to do a for-loop on all files within a directory...

pseudo-code example:

for ($filename is each possible file)
{
Do stuff with the $filename;
}


What should the for-loop look like or is there another way to acces each file seperately?

Loop Every Ten Seconds
Is it possible to get something to repeat every x number of seconds, like cron but i need it as a function of the actual script.

Loop Through A List
I want to loop through a list and display it in a table with three columns per row, yet haven't had much luck in doing so. I was thinking if would be something like so Quote:

for ($x = 0; $x < mysql_num_rows($query); $x++) {
if ($x / 3) { ?>
<tr> <? } ?><td> Blah </td></tr></table>

Well, that's as much as I know so far.

For Loop And Arrays
I'm trying to run through an array which has each value in element 1-3. For example:

for($x=1;$x<3;$x++){
echo "$array[$x]"
}

Is this not valid code, because it doesn't seem to work. Also, is there another way to do this?

2 Values In A Loop
In a guestbook the im building, I want to set the <tr> bgcolor in a difrent color each msg (the same effect is also here in these forums).The code is a while loop (to get mysql results). How can I do it ?

While Loop From Array
I have an array, let's call it $namesarray which is full of names. How do I create a loop such as a while loop to repeat for every value (name) in the array?

Conditional Loop
I have one table, "players" that has player info including start season (year). I then have a table for each season with player stats, i.e. games played etc called stats200X where X is the year obviously.

I have no problem with extracting data from the current year, but what I would like to do is get data from previous years too. I was thinking about doing something like:

(year) - (startyear), which for example could be 3.

I don't know where to begin, but I want to be able to loop though previous years so that, if tables exist of course, I could do

select * from stats{Y -1}
display
select * from stats{Y -2}
etc

Would I need to hard code this or is there a way of doing this though a conditional loop?


For Loop Problems
Anyway, I'm trying to make it so that 3 columns of links will show up
on a page so

link1 link2 link3
link4 link5 link6
link7 link8 link9

etc. I've got this code for it, but I get an error message if the number of
links in the database isn't an integer divided by three. So if it's not
3,6,9,12, etc. I get

Warning: mysql_result(): Unable to jump to row 4 on MySQL result index
3 in mydirectory/loop.php on line 14

I'd like it to stop if there is a number not divisible by 3, but I'm
guessing it just keeps going.

here's the code.

for ($i = 0; $i < mysql_num_rows($result);)
{
echo '<table align="center" cellpadding="0" cellspacing="0"
width="90%"<tr>'
for ($a = 0; $a < 3; $a++)
{
$articlename = mysql_result($result, $i, "articlename");
$articletitle = mysql_result($result, $i, "articletitle");
$i++;
echo '<td width="33%" align="center" valign="top"<p><a
href="/articles/' . $articlename . '/articles.php">' . $articletitle
..'</a></p</td>'
}
echo '</tr></table><br>'
}

Stop A For Loop
I have been messing around with a tutor I found on phpFreaks.com (http://www.phpfreaks.com/tutorials/73/0.php) and what I would like to know is how to stop the for loop for the other pages going over 10.

This it the pices of code which loops to links to the other pages but I don't want it to loop any more than 10 times? PHP Code:

Loop Issues
I am having trouble trying to figure out how I should compose this loop to
give me ALL the results I want.
Below are my queries. I am querying two different databases to pull in
records that match the requested $id. I am then putting the result into a
$variable and also counting the number of rows returned. These queries work...

Print Before Loop
Why cant i get this to print before the while loop

if(mysql_num_rows($sql) == 0);
print($countie);
while ($row = mysql_fetch_array($boo)){

simply results in error

Parse error: parse error, unexpected T_ELSE in /content/search.php on line 42

Getimagesize Loop?
Not sure if this is possible or I am going about this is the correct way?

<?php
list($width, $height, $type, $attr) = getimagesize("images/makers/16/tn/anthologia-mis16-10.jpg");
echo "Image width " .$width;
echo "<BR>";
echo "Image height " .$height;
echo "<BR>";
echo "Attribute " .$attr;
?>

what I would like to do is calulate all the image widths for the following recordset so each user would output a different value

for example there is 16 images for mis_id 16 so the total width would be 2080px, the reason I need this value is because I have a image scroller but this requires a width: as each members gallery will be different

<?php
$colname_gallery = "-1";
if (isset($_GET['mis_id'])) {
$colname_gallery = $_GET['mis_id'];
}
mysql_select_db($database_mis, $mis);
$query_gallery = sprintf("SELECT * FROM members_gallery WHERE user_id = %s", GetSQLValueString($colname_gallery, "int"));
$gallery = mysql_query($query_gallery, $mis) or die(mysql_error());
$row_gallery = mysql_fetch_assoc($gallery);
$totalRows_gallery = mysql_num_rows($gallery);
$galleryimage=mysql_result($gallery,$i,"gallery_image");
?>

While Loop And If - Else Structure
Its simple code but I cant get it working. What I need is code for the following:
A while loop that runs 200 times, I want it to have an if-else statement inside it. This if-else will check a database entry to see if it is equeal to 1 or zero.

if it is equal to 1 it will return the text "booked", if it is equal to 0 it will return the text "available" the database is called "cinema" and the table is called "booking" with 2 fields: ID (which is an auto_increment primary key) and "status"

The loop wil be checking the value in "status" to see if it is equal to 1 or 0. Also, I want the loop to run 200 times and with each iteration it the control variable goes up 1. The loop control also will refer to the primary key ID. So, the loop will run through checking every table row up through each ID and the corresponding 'status' value. Also, I need the loop to go to a new line after every 20th iteration with a <p> tag or something.

Break After 5 In Loop
I just tried searching this on the forum but i couldn't find what I needed, i know there is a way to insert a break after a certain number of times a loop has been executed i.e. after every 5 times insert <br> But I've tried but the list is only having two per row:?
PHP Code:             

Loop Problem
Can anyone tell me why the totals don't add up? PHP Code:

While Loop Question
I am processing a form submission using POST method. Since some of the
fields are going to be blank and I want to eliminate getting those in
the email sent to me, I have put this code together that seems to work
okay and not throw any errors:

<?php
error_reporting (E_ALL);
while(list($key, $val) = each($_POST)){
if(is_array($val)){

foreach($val as $element)
$mesg .= "$key: $element
";
}
if($val == "") {
unset($key,$val);
}
else
$mesg .= "$key: $val

";
}

$to = 'foo@foo.com'
$subject = 'Info Submission'
$header = "From: $_POST[email]
";

mail($to,
$subject,
$mesg,
$header
);
?>

But what puzzles me is that when I echo the results of $mesg to the
page, it's fine. But when it sends to me in an email, I get an extra
period (.) about five or six lines down on every submission and I can't
find out why.

I know it's not exactly an earth-shaking problem but I'm curious as to
what I may have done wrong so I can correct it and make sure I don't
repeat it in the future.

A Loop Question?
Having more problems with updating contacts. For all that responded to
my previous problem THANKS. I accidentally deleted the posts so I
couldn't do it with a reply.

Anyhoo...

When I send an email to an individual (for example) I include this in my
mail script:

$sql = "INSERT into player_contact values ('', '$player_id', '$_POST
[cont_type]', '$_POST[cont_date]', '$_POST[cont_desc]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
//The primary and auto-increment key is the first value, then player_id
of the individual, contact type, contact date, contact description

However, if I send an email to my entire team (each having a player_id)
I can't get it to insert a contact record for each. This is a problem
with a team of over 100 football players as it takes forever to update
each player individually.

WHILE Loops Twice, Only Want It To Loop Once...
I have a working script, but for some annoying reason, this script for counting attendance over time for a single member loops through the years twice...any idea why?:
PHP Code:

Print Once In A Loop?
I've been reading the manual now for quite some time and can't find anything like this. Does that kind of function that does this exist? Example: PHP Code:

Multiple While Loop
How would you do a loop inside a loop??? I've tried numerous ways, but have came up empty handed. Here is the code I have. Code:


Copyright © 2005-08 www.BigResource.com, All rights reserved