Log in

View Full Version : PHP/JS on same page?



Atom
05-10-2012, 10:12 PM
I have put a marquee on my home page with some scrolling text. I found some php code that would allow me to place text from a file into the marquee.
Which is this.
<div align="left" ><Font color="#000000" size="+1">
<?php
$myFile = "clientFTP/marquee.txt";
$fh = fopen($myFile, 'r');
$marquee_text = fgets($fh);
fclose($fh);
?>
<Marquee style="border:solid" bgcolor="#FFFFFF" direction="left" scrollamount="10" loop="2" width="90%"><Strong>
<?=$marquee_text?></Strong></Marquee></font></div>

I can run this on my home page by changing it to .php from .html and putting this line in the .htaccess file: DirectoryIndex index.php index.html

That works, but now when the page is displayed, I lose my menu:

<script type="text/javascript" src="ddlevelsfiles/ddlevelsmenu.js">
</script>

How can I keep the menu script working and use the php code above?

ApacheTech
05-10-2012, 10:30 PM
The <marquee> tag has been depreciated. The same effect can be achieved through many other means, but you shouldn't use the <marquee> tag.

Same with the <font> tag. Use CssClasses rather than the <font> tag for the formatting of text.

EDIT - You also have a lot of other minor depreciated markup. Can you post the whole page source code so we can get a better idea of what's going on.

traq
05-10-2012, 11:58 PM
same with the short echo tag ( <?= ). :)

the PHP portion of your script should look like so (assuming the file path is correct):
<?php
// PHP code should _always_ go at the top of the page.
// It is best practice to not send anything to the browser (echo, closing php tags, etc.)
// until _ALL_ of your PHP is finished.

$myFile = "clientFTP/marquee.txt";
$marquee_text = file_get_contents( $myFile );
// you might also want to check that the file was read successfully,
// and provide "default" text if there was a problem.

// later . . .
?>
<!--html begins here-->

<div class="marquee"><?php print $marquee_text; ?></div>
You might look at Remy Sharp's marquee script (http://remysharp.com/2008/09/10/the-silky-smooth-marquee/) to achieve the effect you want.

(Don't be confused by the fact that his demo uses the <marquee> tag - the javascript removes it and replaces it with a <div> anyway. But -deprecated or not- it does provide a "fallback" for users that have javascript turned off.)

Atom
05-11-2012, 12:28 AM
I often find that when I ask for help with this kind of stuff, people seem to be more interested in telling me why my coding is archaic or clumsy rather than offering meaningful assistance. I'm not a programmer, but I can usually find some examples of what I want and work through it. I don't care if the coding is up to date or elegant, I just want it to work. Here is the page with the marquee I am trying to automate. I think it looks and works fine, but I would like to be able to load the text from a file rather than have to edit the code directly.

http://www.siftradingsystems.com/

I'll take a look at the link and see if I can get where I want from there.

Thanks for your help.

keyboard
05-11-2012, 01:31 AM
I don't think you understand why we're telling you about your out-dated code. It may work fine in whatever browser you're using (I believe IE), but it can cause issues in other browsers (Compatiblity issues). You should always make your code completly cross-browser compatible because other wise you risk losing part of the potential market out there that wont be able to use your website properly.

Also traq posted an answer to your original question (or at least enough to get you started).

traq
05-11-2012, 01:59 AM
indeed. your code being "up to date" or "elegant" has nothing to do with being nitpicky, and everything to do with making your code "work." Sometimes, the most "meaningful assistance" is to suggest an alternative approach.

What happens if your code moves to a server (a new host, perhaps) that doesn't have the short tag enabled? Everything will stop working, and you'll have to go back and find every single <? ?> and change it.

What happens if someone views you page in a browser that throws your page into quirks mode because of some deprecated element in the markup? It could break other things on the page (like scripts, for example), and it might be very difficult to discover the cause of the problem.

My suggestion that your php code be separated from your html markup is all about practicality, as well: it's much, much easier to manage, update, and find and fix bugs that way.

(It also makes you programs more flexible and robust, and helps you become a better programmer, but I didn't think that aspect was strictly relevant to your question. Things like deprecated elements and attributes certainly are relevant.)

I can't tell from the snippet you posted, but it's entirely possible that the way you inserted the PHP block into your html page caused some kind of syntax error that's throwing a wrench into the works.

What does your output HTML look like?

ApacheTech
05-11-2012, 02:33 AM
I apologise for the way I came across. With depreciated code, there is a chance that your site will not look the same when it goes live than in a test environment. This is why I placed the caveat at the end asking for more code to work with. Archaic code can cause massive complications for debugging, and in forums like this we see archaic code every day that is actually the cause of the problems the questioner is facing. In a lot of circumstances, getting the code up to date and cross browser compatible is half of the battle. It may sound counter intuitive but as soon as the problem can be replicated on more than one browser, we can start to pick through and find the solution. Before that point it could just be bad markup that is causing the problem.

I hope this helps your understanding.

Check out http://validator.w3.org/check?uri=www.siftradingsystems.com for an idea of how to streamline and best manage your code.

Atom
05-11-2012, 03:26 AM
Just want to say thanks again for your help and sorry if I seemed ungrateful. I've been given a great deal of help here for which I'm most appreciative. I think I'm just to put the marquee idea aside for a while as it beginning to look a bit more complicated that I care to have to deal with.

traq
05-11-2012, 05:21 AM
Don't give up! Your problem probably isn't really too difficult to solve. We're more than happy to help you through it!

ApacheTech
05-11-2012, 01:15 PM
For your consideration:

There was good reason why the <marquee> tag was depreciated that you might want to think about for your own site.

* The moving text on the screen was found to detract from the rest of the site, causing users to focus mainly on one area of the screen. This was found to have a detrimental effect to the site as a whole.

* Due to the way <marquee>s were animated, it caused very choppy movements across the screen. In extreme cases this was found to freeze the website and the browser and the flickering effect of the text as it moved was found to cause proplems for epileptic users on rare occasions.

* Marquees caused major problems for screen readers and other methods of making webpages easier to use for disabled users. There was no indication of where in the outline of the page the <marquee> should be, this caused screen readers to interpret all <marquee> tags as Headings.

I understand how you are trying to use the <marquee>, you're one of the rare cases who is using it for it's initial intended purpose; to act as a tickertape style news reel. There are some very effective methods of achieving the same effect in a valid way.

This is one example:


<form name="marquee">
<input name="text" value=" Whatever text you type here will scroll">
</form>

<script type="text/javascript">
//<!--
/* Text box marquee by Dave Methvin, Windows Magazine
May be used/modified if credit line is retained */

var ScrollSpeed = 100;
var ScrollChars = 1;

function ScrollMarquee() {
window.setTimeout('ScrollMarquee()',ScrollSpeed);

var msg = document.marquee.text.value;
document.marquee.text.value = msg.substring(ScrollChars) + msg.substring(0,ScrollChars);
}

ScrollMarquee();

//-->
</script>

Atom
05-11-2012, 02:48 PM
For your consideration:

There was good reason why the <marquee> tag was depreciated that you might want to think about for your own site.

* The moving text on the screen was found to detract from the rest of the site, causing users to focus mainly on one area of the screen. This was found to have a detrimental effect to the site as a whole.

* Due to the way <marquee>s were animated, it caused very choppy movements across the screen. In extreme cases this was found to freeze the website and the browser and the flickering effect of the text as it moved was found to cause proplems for epileptic users on rare occasions.

* Marquees caused major problems for screen readers and other methods of making webpages easier to use for disabled users. There was no indication of where in the outline of the page the <marquee> should be, this caused screen readers to interpret all <marquee> tags as Headings.

I understand how you are trying to use the <marquee>, you're one of the rare cases who is using it for it's initial intended purpose; to act as a tickertape style news reel. There are some very effective methods of achieving the same effect in a valid way.

This is one example:


<form name="marquee">
<input name="text" value=" Whatever text you type here will scroll">
</form>

<script type="text/javascript">
//<!--
/* Text box marquee by Dave Methvin, Windows Magazine
May be used/modified if credit line is retained */

var ScrollSpeed = 100;
var ScrollChars = 1;

function ScrollMarquee() {
window.setTimeout('ScrollMarquee()',ScrollSpeed);

var msg = document.marquee.text.value;
document.marquee.text.value = msg.substring(ScrollChars) + msg.substring(0,ScrollChars);
}

ScrollMarquee();

//-->
</script>


Well as it turns out, today I had a client inquiry because of the marquee scroll, so this gives me incentive to make it work and work well.

I did read about the problem of choppy text, although I didn't think mine looked too bad, but this is probably very depend on the individual.

I have been using the Ajax ticker for a long time very effectively, so I suppose one option would be for me to just adapt that since I already know how to implement it. I'd just have to figure out how to make it scroll as opposed to blink.

I'll be looking over the other suggestions as well.

Thanks.

Tom

traq
05-11-2012, 02:53 PM
you're correct that the smoothness of the marquee will vary dramatically across browsers/OS/CPU load/probably lots of other factors.

If you didn't look at Remy Sharp's script yet, I'd still recommend it - it can be implemented very unobtrusively (though I would still recommend "cleaning up" your markup, you don't even really need to to get it to work).

Atom
05-11-2012, 03:50 PM
you're correct that the smoothness of the marquee will vary dramatically across browsers/OS/CPU load/probably lots of other factors.

If you didn't look at Remy Sharp's script yet, I'd still recommend it - it can be implemented very unobtrusively (though I would still recommend "cleaning up" your markup, you don't even really need to to get it to work).

Do you think it is significantly better than the Ajax ticker?

traq
05-11-2012, 04:49 PM
are you talking about this one (http://www.dynamicdrive.com/dynamicindex17/rsstickerajax/index.htm) or this one (http://www.dynamicdrive.com/dynamicindex2/ajaxticker.htm), or something else? (I don't see that either of those scripts have a scrolling feature.)

But to answer your question, do you need the AJAX functionality? or, are you going ahead with using PHP to define the scroller contents?

Remy's would be very easy to implement, considering how your markup is currently arranged. But if you want to be able to fetch the contents via ajax, your "ajax ticker" script might be more appropriate.

Atom
05-11-2012, 05:08 PM
are you talking about this one (http://www.dynamicdrive.com/dynamicindex17/rsstickerajax/index.htm) or this one (http://www.dynamicdrive.com/dynamicindex2/ajaxticker.htm), or something else? (I don't see that either of those scripts have a scrolling feature.)

But to answer your question, do you need the AJAX functionality? or, are you going ahead with using PHP to define the scroller contents?

Remy's would be very easy to implement, considering how your markup is currently arranged. But if you want to be able to fetch the contents via ajax, your "ajax ticker" script might be more appropriate.

I'm using the second one. I looked at Remy's code and I do not understand how to implement it nor how it would be able to read the text from a file.

traq
05-11-2012, 05:23 PM
I'm using the second one.Are you getting it to scoll simply by using the <marquee> element? Or is there an option in the script I'm missing?

I looked at Remy's code and I do not understand how to implement itFirst off, it's a jquery plugin. If you're not using jQuery (and don't want to), then it might not be the choice for you.

Otherwise, basic usage (using your existing markup) would be something like:
<!DOCTYPE html>
<head>
<script src="/path/to/your/jquery.js"></script>
<script src="/path/to/your/jquery.marquee.js"></script>
</head>
<body>
<marquee style="border:solid" bgcolor="#FFFFFF" direction="left" scrollamount="10" loop="2" width="90%">
<strong>marquee text goes here</strong>
</marquee>
<script>
$( "marquee" ).marquee();
/* that's all there is too it */
</script>
</body>
</html>


nor how it would be able to read the text from a file.
It does not.

As I said, this would be a good solution to your marquee problem, assuming you don't need the ajax functionality (i.e., you decided to use PHP to write the contents of the marquee).

If you want to use ajax to write the content of the marquee, then you'd need to use an ajax script -- though, this doesn't mean you couldn't use Remy's script to make the marquee look and function better afterwards.

Atom
05-11-2012, 07:48 PM
Are you getting it to scoll simply by using the <marquee> element? Or is there an option in the script I'm missing?
First off, it's a jquery plugin. If you're not using jQuery (and don't want to), then it might not be the choice for you.

Otherwise, basic usage (using your existing markup) would be something like:
<!DOCTYPE html>
<head>
<script src="/path/to/your/jquery.js"></script>
<script src="/path/to/your/jquery.marquee.js"></script>
</head>
<body>
<marquee style="border:solid" bgcolor="#FFFFFF" direction="left" scrollamount="10" loop="2" width="90%">
<strong>marquee text goes here</strong>
</marquee>
<script>
$( "marquee" ).marquee();
/* that's all there is too it */
</script>
</body>
</html>


It does not.

As I said, this would be a good solution to your marquee problem, assuming you don't need the ajax functionality (i.e., you decided to use PHP to write the contents of the marquee).

If you want to use ajax to write the content of the marquee, then you'd need to use an ajax script -- though, this doesn't mean you couldn't use Remy's script to make the marquee look and function better afterwards.

No, it's not scrolling, just blinking. Here's one of the pages:
http://www.siftradingsystems.com/SixSystemTrades_01.html

I simply assumed that it had a scrolling feature, but having looked, I don't see one.

So far, I haven't been able to find anything more suitable, then just editing the page itself and using the old fashioned marquee.

traq
05-11-2012, 11:06 PM
Perhaps an example (http://jsfiddle.net/JN5yS/).

Atom
05-12-2012, 04:30 AM
Perhaps an example (http://jsfiddle.net/JN5yS/).

Right. I can't do much of anything if it's not completely cook book.

http://www.siftradingsystems.com/BlankPage_New3.php

That is a better looking scroll, and the page construction looks generally correct. I'll have to try it with the index page and see if everything still works.

Thanks.

ApacheTech
05-13-2012, 01:18 AM
Marquees are now available in CSS3!



.marquee {
overflow-x: -webkit-marquee;
-webkit-marquee-direction: ahead|auto|backwards|down|forwards|inherit|left|reverse|right|up;
-webkit-marquee-increment: small|medium|large;
-webkit-marquee-repetition: {number};
-webkit-marquee-speed: slow|normal|fast;
-webkit-marquee-style: alternate|inherit|none|scroll|slide;
font-size:1.4em;
}

However, this site (http://kougiland.com/css3-marquee.html) shows why you should be careful when using it.

Also, there is no onmouseover="this.stop()" facility.

Atom
05-13-2012, 04:24 AM
I'm back to my original problem, which is being able to load the scroller text from file, but losing the sidebar menu. This happens when I convert the page from .html to .php which is necessary to run the php code. I want the page to look something like this

http://www.siftradingsystems.com/BlankPage_New3.php

instead of this

http://www.siftradingsystems.com/BlankPage_New4.php

If you look at the source code you will see the difference in how the sidebar menu is constructed, I don't want to use the one method because I then have to edit every page which has this menu. I didn't build these, but I think when they were first built there were two ways to make the menu and I'm not sure why, but eventually I discovered I could just use the menu.shtml which greatly simplified things when I wanted to add a link to a new page.

Could I use some php code to construct the menu?

Never mind, I got it. Just had to run php as SSI.

http://www.siftradingsystems.com/BlankPage_New5.html