Forum Moderators: coopster

Message Too Old, No Replies

XML Ticker with php 5.3.5

Can anyone explain this ..? Thanks for any efforts.

         

andylonger

8:56 am on Feb 10, 2011 (gmt 0)

10+ Year Member



Hi,
I have done news ticker using XML. It was working perfectly fine on php4. i just updated to php5.3.5 and i get the following error message. I couldn't figure it out.. can anyone help me regarding this.

error :-Notice: Undefined variable: faction in C:\wamp\www\controlpanel\eventupdate.php on line 9

Notice: Undefined offset: 10 in C:\wamp\www\controlpanel\eventupdate.php on line 48

Here is the code:-


<?php
session_start();
//global $faction;
//$faction ='';
$file = "../Ticker/ticker.xml";

//-------------------------------------------------save to the XML file---------------------------------------------
$strToFile = "";
if(strtoupper(trim($faction)) == "SAVE"){ //line 9
$strToFile = $strToFile."<xml id='xmlNews'>\n";
$strToFile = $strToFile."<display>".$sclTickerStatus."</display>\n";
$strToFile = $strToFile."<ticker>\n";
$arrMessages = split("-,",$messagesArray);
for($i = 0; $i < count($arrMessages); $i++)
$strToFile = $strToFile."<message>".$arrMessages[$i]."</message>\n";
//$strToFile = $strToFile."<message>".$TickerMsg[$i]."</message>\n";
$strToFile = $strToFile."</ticker>\n";
$strToFile = $strToFile."</xml>";

//------------------
if (file_exists($file))
{
unlink ($file);
}

$handle = fopen($file,'a+');

if (fwrite($handle, $strToFile) === FALSE)
{
echo 'View Reports'; //View reports
}

fclose($handle);
}



//-----------------------------------------------------XML Parser---------------------------------------------------
global $currTag, $display, $mArray;
$mArray=array();
$depth =array();

function startElement($parser, $name, $attrs)
{
global $depth,$currTag;
$currTag = strtoupper(trim($name));
//$depth++;
$depth[$parser]++; ///Line 48
}

function characterData($parser, $data)
{
global $currTag, $display, $mArray;
$curData = trim($data);
if(strlen($curData)>0){
if($currTag == "DISPLAY")
$display = strtoupper($curData);

if($currTag == "MESSAGE"){
$mArray[count($mArray)] = $curData;
}
}
}

function endElement($parser, $name)
{
global $depth;
// $depth--;
$depth[$parser]--;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = fopen($file, "r"))) {
echo 'Error Reading Message'; //Error reading Message
}

while ($data = fread($fp, 4096))
{
if (!xml_parse($xml_parser, $data, feof($fp)))
{

echo 'Error Reading Message';

}
}
xml_parser_free($xml_parser);

//----------------------------------------------------------------------------------------------
echo "<script>";
echo "var messages= new Array(".(count($mArray)+1).");";
for($l=0; $l<count($mArray); $l++){
echo "messages[".$l."]='".$mArray[$l]."';";
}
echo "</script>";
?>
<form name="item" action="directing.php?page=events" method="post">
<input name="faction" type="hidden">
<input name="messagesArray" type="hidden">
<table align="center" class="tableForm" width="75%" cellspacing="10">
<tr><td>
Status:</td><td><select name="sclTickerStatus" onChange="submitForm()">
<option value=<?php
echo 'YES';
if($display == "YES")
echo " selected";
?>><?php echo 'YES';?></option>
<option value=<?php
echo 'NO';
if($display == "NO")
echo " selected";
?>><?php echo'NO';?></option>
</select>
</td></tr>
<td valign="top">Event Updates:</td>
<td><textarea id="newmessage" name="newmessage" cols="30" rows="3" wrap="soft" onKeyUp="count(this)"></textarea><br />
<!--<div>char: <span id="cont"><?php echo strlen('$newmessage');?></span>/400</div> -->
<input type="button" name=btnAdd value="Add" onClick="addMessage()"></td>
</tr>
<!---- New Added-->
<tr><td colspan="2"><hr noshade width="70%"></td></tr>

<?php
echo "<tr><td colspan=2>";
echo "<table width=100% border=0 bordercolor='#000000' cellpadding=2 cellspacing=0>";
for($l=0; $l<count($mArray); $l++)
{
echo "<tr class=".($l%2==0?"dark":"light")."><td width=10% align=center valign=center><b>".($l+1)."</b></td><td>".$mArray[$l];
echo "</td><td width=10%>";
if($l != 0)
echo "<a href='javascript:moveUp(".$l.");'><img src='../controlpanel/img/up.gif' border=0> </a>";
if($l != count($mArray)-1)
echo "<a href='javascript:moveDown(".$l.");'><img src='../controlpanel/img/down.gif'' border=0> </a>";
echo "</td><td width=1%><input type=button value='Delete' onClick='deleteMessage(".$l.")'></td></tr>";
}
echo "</table>";
echo "</td></tr>";
?>
</td></tr>
<!--- End of New Added-->
</table>
</form>

<script language="javascript">
function count(textObj)
{
var maxlen = 400;
//alert(textObj.value);
var text = textObj.value;
if(text.length >= maxlen)
text = text.substr(0, maxlen);
textObj.value = text;
document.getElementById('cont').innerHTML = text.length;
//var cont = getElementById('cont');
//alert(cont);*/
}
function cancel(page)
{
location.replace(page);
}
function addMessage()
{
if(document.forms[0].newmessage.value.length > 0)
{
messages[messages.length-1] = document.forms[0].newmessage.value;
submitForm();
}
else
alert("Please Specify a message to add"); //'Please specify a message to add

}
function deleteMessage(loc)
{
for(i = loc; i < messages.length-1; i++)
messages[i] = messages[i+1];
submitForm();
}
function saveMessagesArray()
{
var temp = "";
var temp2 = "";
for(i = 0; i < messages.length; i++)
{
if( messages[i] != null )
{
if(temp.length > 0)
temp = temp + "-,";
temp2 = messages[i].replace(/</g,"(");
temp2 = temp2.replace(/>/g,")");
temp2 = temp2.replace(/&/g,"and");
temp2 = temp2.replace(/-,/g,",");
temp = temp + temp2;
}
}
document.forms[0].messagesArray.value = temp;
}
function moveUp(loc)
{
temp = messages[loc-1];
messages[loc-1] = messages[loc];
messages[loc] = temp;

submitForm();
}
function moveDown(loc)
{
temp = messages[loc+1];
messages[loc+1] = messages[loc];
messages[loc] = temp;

submitForm()();
}
function submitForm()
{
saveMessagesArray();
document.forms[0].faction.value = "save";
document.forms[0].submit();
}
</script>


Wittner

9:27 am on Feb 10, 2011 (gmt 0)

10+ Year Member



if(strtoupper(trim($faction)) == "SAVE"){ //line 9

in PHP5 you must define variables before you use them. You could set up a section at the beginning of your script and define the vars first - that's one option. Defining in PHP5 can be done like:

$faction = '';

From then on you can use the variable.

Wittner

9:29 am on Feb 10, 2011 (gmt 0)

10+ Year Member



Actually, I just see now that you have commented out $faction at the beginning of the script so that's why it's not defined:

//$faction='';

;-)

andylonger

9:54 am on Feb 10, 2011 (gmt 0)

10+ Year Member



Hi,
Thanks Wittner for correcting. Actually i was trying so many ways to fix this. My main concern to save the input data to the XML file and display it on other page which scrolls it.
How about working around the second error
Notice: Undefined offset: 10 in C:\wamp\www\controlpanel\eventupdate.php on line 48

what does its looking for when it says undefined offset

andylonger

10:30 am on Feb 10, 2011 (gmt 0)

10+ Year Member



Can any one explain this ..?

Wittner

11:51 am on Feb 10, 2011 (gmt 0)

10+ Year Member



Undefined offset usually refers to the parameters expected to be input for a function, so in the case of:

function startElement($parser, $name, $attrs)
{
global $depth,$currTag;
$currTag = strtoupper(trim($name));
//$depth++;
$depth[$parser]++; ///Line 48
}

... you have not provided a value for $parser for the startElement function within the braces so that on line 48 $parser has no value to work with. The variables $parser, $name, and $attrs are all 'offsets' and must be given a value for the function to work. The actual error is on line 43 in between the braces. For instance:


function sayHello($name)
{
echo 'Hi ' . $name;
}

Will give an undefined offset error. Whereas...

$name = 'Wittner';
function sayHello($name)
{
echo 'Hi ' . $name;
}

... will work. The offset is the value or values expected by the function. It can't do its job without them,

cheers

Wittner

andylonger

12:01 pm on Feb 10, 2011 (gmt 0)

10+ Year Member



Hi Witter, Can you tell me how can i provide a value. I guess it references to the XML parser calling the handler.

andylonger

12:09 pm on Feb 10, 2011 (gmt 0)

10+ Year Member



i Have gone through this link [php.net...] to figure it out .. But still cant find a fix for this

Wittner

6:14 pm on Feb 10, 2011 (gmt 0)

10+ Year Member



Hi andylonger. I can give you some general PHP help but I'm not familiar with using the PHP XML parser.

andylonger

1:14 pm on Feb 12, 2011 (gmt 0)

10+ Year Member



Thats alright Wittner thanks for ur help..

Have a nice day..!