Page 1 of 1

Javascript in userdefined tag

Posted: Fri Feb 02, 2007 9:40 pm
by Neo
Hi all,
    I am a newbie to CMSMS and I need to migrate a website currently in html and php into CMSMS. I am finding huge difficulties in creating an userdefined tag that connects to the database to display some dynamic content on the page. I was basically trying to use the same html and php code into CMSMS (Not sure whether this is correct or not!!)

Actually the requirement is that there is a small table (one one side of the page) in my main page where the content needs to be taken from the database and refreshed after every fixed period of time (like flash news). So I had thought of creating an userdefined tag and use php, javascript code in that to implement this functionality. The code in the userdefined tag is below.

Code: Select all

require_once('../Connections/gwriDB.php'); 

mysql_select_db($database_gwriDB, $gwriDB);
$query_RsFacts = "SELECT * FROM Facts ORDER BY Facts.`Order`";
$RsFacts = mysql_query($query_RsFacts, $gwriDB) or die(mysql_error());
$row_RsFacts = mysql_fetch_assoc($RsFacts);
$totalRows_RsFacts = mysql_num_rows($RsFacts);

{literal}
<__script__ language="JavaScript1.2">

//IFRAME TICKER- By Dynamic Drive (http://www.dynamicdrive.com)

//configure delay between changing messages (3000=3 seconds)
var delay=15000

var ie4=document.all

var curindex=0
var totalcontent=0

function get_total(){
if (ie4){
while (eval("document.all.content"+totalcontent))
totalcontent++
}
else{
while (document.getElementById("content"+totalcontent))
totalcontent++
}
}

function contract_all(){
for (y=0;y<totalcontent;y++){
if (ie4)
eval("document.all.content"+y).style.display="none"
else
document.getElementById("content"+y).style.display="none"
}
}

function expand_one(which){
contract_all()
if (ie4)
eval("document.all.content"+which).style.display=""
else
document.getElementById("content"+which).style.display=""
}

function rotate_content(){
get_total()
contract_all()
expand_one(curindex)
curindex=(curindex<totalcontent-1)? curindex+1: 0
setTimeout("rotate_content()",delay)
}

window.onload=rotate_content

</__script>
{/literal}

<link href="../Common/GWRI_CSS.css" rel="stylesheet" type="text/css">
</__body bgColor=#F4FDFF text="#990000" link="#990000" vlink="#990000" alink="#990000">

<!--ADD YOUR TICKER CONTENT BELOW, by wrapping each one inside a <DIV> as shown below.-->
<!--For each DIV, increment its ID attribute for each additional content (ie: "content1", "content2" etc)-->

$thiscont = -1 

<!-- ADD TICKER's CONTENT #1 HERE--------------------->

	   do {
	   $thiscont = $thiscont + 1;
	   $thiscontstr = "content" . $thiscont;
      printf("<div id='%s' style='display:'''><p class='InterestingFact'> %s</p></div>", $thiscontstr, $row_RsFacts['Fact']);
	  } while ($row_RsFacts = mysql_fetch_assoc($RsFacts));


        mysql_free_result($RsFacts);
I guess I am doing some basic mistake in implementing this functionality but not sure what is that  :(. Please let me know how should I go about resolving this problem. It would be of great help in resolving this problem.

Thanks a ton in advance,
Neo

Re: Javascript in userdefined tag

Posted: Fri Feb 02, 2007 10:37 pm
by cyberman
UDTs are using PHP pure so {literal} tags wount work. You have to call your javascript via echo or include.

Re: Javascript in userdefined tag

Posted: Mon Feb 05, 2007 12:31 am
by Neo
Hi cyberman,
      Thanks for the reply. But I could not figure out how to use echo inside a UDT. I tried to enclose the javascript code by using echo as below.

Code: Select all

echo"<__script__ language="JavaScript1.2">";
But still I am unable to include javascript code in this fashion. Is there any example or documentation for this? Please let me know if this is the incorrect way to do this.

Also do I need to change the settings in config.php to use these commands (like changing the smarty tags to true)?

Thanks,
Neo

Re: Javascript in userdefined tag

Posted: Mon Feb 05, 2007 1:23 am
by Neo
Hi cyberman,
        The command echo worked to include the javascript code  :). Now that I have created this UDT and included in the template, do I need any additional changes to implement this database connection through the PHP code? This is because now when I tried to include this UDT code in the template, I got the error

mysql_pconnect()
[function.mysql-pconnect]: Lost connection to MySQL server during query in /..../lib/gwriDB.php

Please let me know if I need to do any other modifications for the database connection.

Thanks,
Neo

Re: Javascript in userdefined tag

Posted: Mon Feb 05, 2007 6:40 am
by cyberman
Neo wrote:         The command echo worked to include the javascript code  :).
That's what I mean  ;).
Please let me know if I need to do any other modifications for the database connection.
Don't know source from gwriDB, but think the script tries to open/close a connection to database. I'm not a coder for real but think that's not needed. CMSms do it for you, you need only

Code: Select all

global $gCms;
$db =& $gCms->GetDb();
in front of the script.