Forum Moderators: coopster

Message Too Old, No Replies

Interrupt while loop?

Wanting to insert one line in the middle of a loop

         

doodlebee

10:28 pm on Apr 5, 2007 (gmt 0)

10+ Year Member



I'm trying to alter some code I have that aids in selecting stylesheets. Basically, this code will look to see what page you're on, and serve up the right stylesheet for that page. (For the record, it's ZenCart.)

The code looks like so:


while(list ($key, $value) = each($sheets_array)) {
//echo "<!--looking for: $value-->\n";
$perpagefile = $template->get_template_dir('.css', DIR_WS_TEMPLATE, $current_page_base, 'css') . $value . '.css';
if (file_exists($perpagefile)) echo '<link rel="stylesheet" type="text/css" href="' . $perpagefile .'" />'."\n";
$perpagefileie = $template->get_template_dir('.css', DIR_WS_TEMPLATE, $current_page_base, 'css') . $value . '.css';
if (file_exists($perpagefileie)) echo "\n".'<!--[if IE]>'."\n".'<link rel="stylesheet" type="text/css" href="' . DIR_WS_TEMPLATE . 'css' . $value .'_ie.css" />'."\n".'<![endif]-->'."\n\n";
}
"\n".'<link rel="stylesheet" href="alternate_styles.php" type="text/css" />';

the output of the above code looks like so:


<link rel="stylesheet" type="text/css" href="/css/stylesheet.css" />
<link rel="stylesheet" type="text/css" href="/css/stylesheet_category.css" />
<link rel="stylesheet" type="text/css" href="/css/stylesheet_shopping_cart.css" />
<link rel="stylesheet" type="text/css" href="/css/c_12_15.css" />
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="includes/templates/Natalie/css/c_12_15_ie.css" />
<![endif]-->
<link rel="stylesheet" href="alternate_styles.php" type="text/css" />

What I *want* to do is to place the "alternate_styles.php" line *between* the two secions of code, so that the alternate_styles.php line comes right before the "if IE" conditional.

I've tried several options. I can get the alternate styles where I want it to be, but the result is 1) I either get many lines of the "alternate_styles.php" line (I'm sure it's because it's become part of the loop, so every time a line is printed, the "alternate" line is also printed - which I don't want - I just want it to appear *once*, right before the "if IE" conditional) - or 2) it appears as desired, but the IE conditional disappears.

Is there any way I can "pause" or interrupt the while loop to insert a line of static text in a particular place? Someone suggested setting a flag, but I must be doing it wrong, because it's having no effect whatsoever.

I'd appreciate any help with this. Thanks!

cmarshall

12:45 am on Apr 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why don't you just push it into the array before the loop?

That way, it gets output as part of the standard process.

doodlebee

1:16 am on Apr 6, 2007 (gmt 0)

10+ Year Member



Thanks marshall.

I feel like an idiot - that would seem to be the simplest solution.

Problem is, I'm pretty new at arrays, and I can't seem to get that to fit into my array.

But I'll keep trying - in the meantime, if anyone has any tips for me, that'd be great :)

EDIT - meaning, I can get the *file* to show up, but it's a conditional comment, so I'm trying to figure out how to add the <!--[if IE]> stuff before and after the filename - *that* is my issue.

doodlebee

1:09 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



Well, after a good night's sleep (well, as much as you can have with a teething 8-month-old...) I woke up realizing what a dolt I am. All I need to do is add the "alternate_styles.php" into the array, and I'll be set. DUH.

However, for some reason, it *refuses* to accept any input into the array. It makes no sense to me at all. I started over fresh, and it's just driving me batty.

The code, in its entirety, reads as such:


$manufacturers_id = (isset($_GET['manufacturers_id']))? $_GET['manufacturers_id'] : '';
$tmp_products_id = (isset($_GET['products_id']))? (int)$_GET['products_id'] : '';
$tmp_pagename = ($this_is_home_page)? 'index_home' : $current_page_base;
$sheets_array = array('/' . $_SESSION['language'] . '_stylesheet',
'/' . $tmp_pagename,
'/' . $_SESSION['language'] . '_' . $tmp_pagename,
'/c_' . $cPath,
'/' . $_SESSION['language'] . '_c_' . $cPath,
'/m_' . $manufacturers_id,
'/' . $_SESSION['language'] . '_m_' . (int)$manufacturers_id,
'/p_' . $tmp_products_id,
'/' . $_SESSION['language'] . '_p_' . $tmp_products_id
);
while(list ($key, $value) = each($sheets_array)) {
//echo "<!--looking for: $value-->\n";
$perpagefile = $template->get_template_dir('.css', DIR_WS_TEMPLATE, $current_page_base, 'css') . $value . '.css';
if (file_exists($perpagefile)) echo '<link rel="stylesheet" type="text/css" href="' . $perpagefile .'" />'."\n";
}

the "$sheets_array = array" is where i need to add in my stuff. All I need to do is add one stinking line to change it from this:

'/c_' . $cPath, 
'/' . $_SESSION['language'] . '_c_' . $cPath,

to this:

'/c_' . $cPath, 
'/alternate_styles.php',
'/' . $_SESSION['language'] . '_c_' . $cPath,

But when I do that, it *totally* ignores my new addon to the array. I thought maybe it would be the "." in there, so I've tried backslashing it out to setting it as a variable, and calling the variable into the array - but nothing. Once - just to see what happened, I changed "'/c_' . $cPath" to "'/cd_' . $cPath" - and just adding the "d" made *that* line disappear. Just by adding a letter!

Am I nuts? That just doesn't seem like it should be happening. What am I missing? It's gotta be something - because from what *I* see, I should be able to add a line that says "Spank me hard!" into the array, and it'll show up in my code - but it's not.

Ahh, the little things that drive you batty. (Maybe I *do* need some sense "spanked" into me! LOL)

doodlebee

1:22 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



No, no -

Man I hope you all don't mind me "Talking out loud" here - I just figured out why I can't see it. That array looks for whatever page you're on, and pulls out the stylsheet for that page. I want my "alternate_styles.php" to show up on *every* page - and putting it in that array means it's looking for the associated page for it - and there is none. SO it won't appear.

DUH.

Sorry - I'm sure I'll figure this out at some point!

jatar_k

1:40 pm on Apr 6, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



does it have to be in that exact spot? you could just append it after then loop

doodlebee

1:55 pm on Apr 6, 2007 (gmt 0)

10+ Year Member



does it have to be in that exact spot?

Yeah, it does.

The reason is that, one *one* particular page, I have to have an IE conditional appear. The "alternate_styles.php" file already holds my stuff for other browsers, as well - so I can't add the single piece of style code I need to that file, or it would screw the rest of the site up in IE (and I only need to fix one page).

When I do it one way, then the IE conditional comes up as it should, but the the "alternate_styles.php" file gets called in and overwrites the conditional. So the "alternate "*must* be after the regular stylesheet, but before the conditional.

But - and it's funny, because I was *just* getting ready to report back - I figured it out.

I made a new while{} statement with a whole new array. Then I hard coded the alternate after the original loop, then put the new while{} after that.

It's now working.

I'm sure there's a better way to do it, but I'm just happy it's working!