Forum Moderators: coopster

Message Too Old, No Replies

Help on a foreach loop

I think thats what is needed

         

bobnew32

6:57 pm on Oct 22, 2003 (gmt 0)

10+ Year Member



My only downside to knowing php is that I am not very good at loops, whatsoever. For this certain help on foreach loop, it would create a navigation for a multi-page article I am viewing. If your on page two of three, it would go

<a href="Link To page 1">Page 1 Name</a><br>
Page 2 Name<br>
<a href="Link to page 3">Page 3 Name</a>

So that it would show links to every page that is connected to that certain $article_id in the page url, except for the page your currently viewing.

Heres what I have so far:

$sql= "Select * from article_hubs";
$result = mysql_query($sql);
$article_hub_row = mysql_fetch_array($result);

$pages= $article_hub_row['pages'];

Then I have a table called review_pages which contains the "name" of the various pages in that article. I'm thinking a

while($row = mysql_fetch_array($result))

will need to be used. I have heard of "stopping" the foreach loop, and I think that is what will be needed to stop the loop when it reaches the current page that we're viewing and to just print the name of it.

Please someone help me, its just too much new php stuff i'm trying to process all at the same time hehe.

DrDoc

7:04 pm on Oct 22, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you can always do something like this:

$foobar = 0;
while($row = mysql_fetch_array($result) && $foobar==0) {
//do stuff
if(...) {
//do stuff
$foobar = 1;
}
}

coopster

8:56 pm on Oct 22, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



break [us2.php.net] ends execution of the current
while
control structure. For example:

while($row = mysql_fetch_array($result)) {
//do stuff
if(...) {
//do stuff
break;
}
}

But in order to help you out, we are going to need a few more details:
  1. Are the article_hubs and review_pages tables linked via the article_id column?
  2. What columns are in each table?
  3. Does the review_pages table contain a row for each page in each article?

bobnew32

2:27 am on Oct 23, 2003 (gmt 0)

10+ Year Member



1. Yes

2. The review_hubs table contains data on how many "pages" are in the review, the "name" of it, "id" of it, and thats mainly that is needed to tell. The review pages table contains the "name" of the page, id of it, and id of the review hub it belongs to. Thats really onl that needed for that table.

3. Yes

I have my page assigned like so, review.php?id=1&page=5 . You get the idea. Thx

coopster

12:31 pm on Oct 23, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I think I understand your table structures, so I'll take a shot here (I've assigned my own field names since I don't know yours):

Table: review_hubs
----------------------------------
review_id review_name nbr_of_pages
========= =========== ============
........1.....Review1............6
........2.....Review2............2
........3.....Review3............1


Table: review_pages
----------------------------------
review_id page_id page_name
========= ======= =========
........1.......1.....Page1
........1.......2.....Page2
........1.......3.....Page3
........1.......4.....Page4
........1.......5.....Page5
........1.......6.....Page6
........2.......1.....Page1
........2.......2.....Page2
........3.......1.....Page1

I have my page assigned like so, review.php?id=1&page=5


// make sure $_GET data is what you expect (numbers, etc.)
if (!is_numeric($_GET['page'])) exit('page must be numeric');
$sql =
"SELECT * FROM review_pages
WHERE review_id = '" . $_GET['id'] .
"' ORDER BY page_id";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$page_names[$row['page_id']]=$row['page_name'];
}
//if ($_GET['page'] < 0 or $_GET['page'] > count($page_names)) exit('Invalid page number');
$previous_page = (isset($page_names[$_GET['page'] - 10]))? $page_names[$_GET['page'] - 10] : '';
$current_page = $page_names[$_GET['page']];
$next_page = (isset($page_names[$_GET['page'] + 10]))? $page_names[$_GET['page'] + 10] : '';
if ($previous_page) {
print "<a href=\"Link To page " . ($_GET['page'] - 10) . "\">$previous_page</a><br>";
}
print "$current_page<br>";
if ($next_page) {
print "<a href=\"Link To page " . ($_GET['page'] + 10) . "\">$next_page</a><br>";
}

This will give you a key=value array of your page_id and page_name. Use an if statement to build the links you need. I have to run, otherwise I'd show you how. If you need help, post again and somebody will jump in.
<edit>I'm back and threw the extra code in here. I am assuming you are numbering your pages sequentially. Anyway, tweak it as you see need fit.</edit>

Notice I didn't use the total number of pages field whatsoever. You wouldn't have to store the total number of pages in the review_hubs table. It's tough to try and synchronize data this way, therefore I try not to practice this type of data storage (totals in one file based on number of detail records in another). I realize somebody is going to come along and argue about the performance of seeing the total in this single row versus counting rows every time, but trust me, somewhere down the line this practice will come and bite you in the rear -- usually with payroll or some other critical financial application. Somebody will let you know about it though and then there's egg on your face as well as writing a new application to sychronize your totals every night. Yeah, sure, I agree, real nice trade-off for performance...(sarcasm inserted purposely).

If it isn't an exact fit, at least you have a start. Post again if you need more help.

Regards -- coopster