xml parser using php from a link - php

i have the following code in PHP
$link ="http://ws.audioscrobbler.com/2.0/?method=&user=xgayax" .
"&api_key=b25b959554ed76058ac220b7b2e0a026";
$xml = #simplexml_load_file($link);
$tracks = $xml->recenttracks->track;
for ($i = 0; $i < 3; $i++) {
$playingnow = $tracks[$i]->attributes()->nowplaying;
$name = $tracks[$i]->name;
$artist = $tracks[$i]->artist;
$url = $tracks[$i]->url;
$date = $tracks[$i]->date;
$img = $tracks[$i]->children();
$img = $img->image[0];
echo "<a href='" . $url . "' target='TOP'>";
if ($nowplaying == "true") {
echo "Now playing: ";
}
echo "<img src='" . $img . "' alt='album' />
$artist . " - " . $trackname . " # " . $date . "
</a>
";
}
and i got the following error
Parse error: syntax error, unexpected '#', expecting ',' or ';' on line 31
any solution for this problem???

Your last lines have a error, the correct code is:
echo "<img src='" . $img . "' alt='album' />" .
// ^^^ missing
$artist . " - " . $trackname . " # " . $date . "
</a>
";

Related

SQL SELECT not working within Array MYSQL_ASSOC

I can't seems to find the error in below script. i have been checking for while now. sorry i'm really new to SQL and PHP.
$tCompany_SQLselect = "SELECT ";
$tCompany_SQLselect .= "ID, preNAME, Name, RegType ";
$tCompany_SQLselect .= "FROM ";
$tCompany_SQLselect .= "tCompany ";
$tCompany_SQLselect_Query = mysql_query($tCompany_SQLselect);
$index = 1;
while ($row = mysql_fetch_array($tCompany_SQLselect_Query, MYSQL_ASSOC)) {
$preNAME = $row['preNAME'];
$Name = $row['Name'];
$RegType = $row['RegType'];
echo $index.".".$preNAME."".$Name."".$RegType" <br />;
$index++;
}
The problem is in the way you concatenated the variables.
You forgot a dot and a quote. Change .$RegType" <br />; to . $RegType . "<br />";
echo $index . "." . $preNAME . " " . $Name . " " . $RegType "<br /> ;
^ dot ^ quote
Change it to:
echo $index . "." . $preNAME . " " . $Name . " " . $RegType . "<br />";
You can remove the space in the " " if you want.
first: you can use:
$tCompany_SQLselect_Query = mysql_query($tCompany_SQLselect) or die(mysql_error());
this will allow you to debug the error.
second: you can use:
$row = mysql_fetch_assoc($tCompany_SQLselect_Query)
to shorten the syntax.
The echo line should be:
echo $index.".".$preNAME." ".$Name." ".$RegType." . "<br />";
You were missing quotes around <br />, and the . for concatenation before it.

Pesky Apostrophes in Database results

Okay... to make a long story short... here is my code...
<?php
$con = mysql_connect($db_server_name,$db_username,$db_password);
if (!$con)
{
echo "0";
}
mysql_select_db("" . $db_database_name . "", $con);
$result = mysql_query("SELECT * FROM sched_posts
WHERE user_id='$user_id'");
while($row = mysql_fetch_array($result))
{
$post_id = $row['ID'];
$post_year = $row['post_year'];
$post_month = $row['post_month'];
$post_day = $row['post_day'];
$post_hour = $row['post_hour'];
$post_minute = $row['post_minute'];
$post_privacy = $row['post_privacy'];
$post_message = $row['post_message'];
echo " {";
echo " id: " . $post_id . ",";
echo " title: ' " . $post_message . "',";
echo " start: new Date(" . $post_year . ", " . $post_month . "-1, " . $post_day . ", " . $post_hour . ", " . $post_minute . "),";
echo " allDay: false";
echo " },";
}
?>
When returning results, the post_message sometime's comes back with apostrophes in it. How can I get those results to appear as \' instead of just ' (in other words... with a backslash in front of it)?
PS.. I know some of this code looks unnecessary but please try to ignore that.... this is only setup this way for some testing that i am doing for facebook SDK results (for example, the identifiers inside of the WHILE statement).
The problem is, the returned apostrophes are causing the entire thing to go loopy... you know what i mean.
If you convert all those "date partial" columns into a timestamp, you can simply use json_encode():
$ts = mktime($post_hour, $post_minute, 0, $post_month, $post_day, $post_year);
echo json_encode(array(
'id' => $row['ID'],
'title' => $row['post_message'],
'start' => date('r', $ts), // <-- that's a string now
'allDay' => false,
));
JavaScript has no problems using rfc822 formatted dates.
To add backslashes, the function addslashes() would work for this:
http://php.net/manual/en/function.addslashes.php
To encode JSON 100% reliably (especially for fields like this that you can't predict/expect certain values/input), it would be best to use json_encode():
while($row = mysql_fetch_array($result))
{
$post_id = $row['ID'];
$post_year = $row['post_year'];
$post_month = $row['post_month'];
$post_day = $row['post_day'];
$post_hour = $row['post_hour'];
$post_minute = $row['post_minute'];
$post_privacy = $row['post_privacy'];
$post_message = $row['post_message'];
$dateString = ''; // computed date string...
echo json_encode(array("id"=>$post_id,"title"=>$post_message,"start"=>
$dateString,"allDay"=>false));
}
The json_encode() function is designed to generate JSON data but, since JSON is a subset of JavaScript, it's the best alternative to generate dynamic strings. Here's a use example:
<?php
$post_id = 314;
$post_message = <<<EOM
Jim "Big Boy" O'brian wrote:
<strong>Hi</strong>
EOM;
$post_year = 2013;
$post_month = 10;
$post_day = 9;
$post_hour = 17;
$post_minute = 4;
echo "{";
echo " id: " . $post_id . ",";
echo " title: " . json_encode($post_message) . ",";
echo " start: new Date(" . $post_year . ", " . $post_month . "-1, " . $post_day . ", " . $post_hour . ", " . $post_minute . "),";
echo " allDay: false";
echo "},";
... that produces:
title: "Jim \"Big Boy\" O'brian wrote:\r\n\r\n<strong>Hi<\/strong>\r\n"
Please note you have to omit the surrounding quotes; the function adds them for you.

Create text file out of query results

I need to create a text file from a foreach loop (code below) but i'm not sure how. So far I have tried to use fwrite but I'm not to sure how to write that much data to a txt file. I also tried to use file_put_content(), it worked but it displayed all of the HTML and had no line breaks. Any ideas on how to do display it?
Foreach loop
echo "<p>" . 'custom_weapons_v3' . "</p>";
echo "<p>" . '{' . "</p>";
foreach($arr as $key => $a){
echo "<br>";
$check = $a['steamid'];
echo $check;
echo "<br>";
echo "{";
echo "<br>";
foreach($data as $key => $r){
$check2 = $r['steam'];
if($check == $check2){
echo " \"{$r['wepid']}\" //{$r['wepname']}<br />";
echo '{' . "<br />";
echo '"level"' . ' "' . $r['weplvl'] . '"' . "<br />";
echo '"quality"' . ' ' . '"' . $r['weprare'] . '"' . "<br />";
if($r['attr1'] === 0 || $r['attr1'] === ''){
echo '';
}else{
echo '"1"' . ' "' . $r['attr1'] . ' ; ' . $r['val1'] . '"' . "<br />";
}
if($r['attr2'] === 0 || $r['attr2'] === ''){
echo '';
}else{
echo '"2"' . ' "' . $r['attr2'] . ' ; ' . $r['val2'] . '"' . "<br />";
}
if($r['attr3'] === 0 || $r['attr3'] === ''){
echo '';
}else{
echo '"3"' . ' "' . $r['attr3'] . ' ; ' . $r['val4'] . '"' . "<br />";
}
if($r['attr4'] === 0 || $r['attr4'] === ''){
echo '';
}else{
echo '"4"' . ' "' . $r['attr4'] . ' ; ' . $r['val4'] . '"' . "<br />";
}
if($r['attr5'] === 0 || $r['attr5'] === ''){
echo '';
}else{
echo '"5"' . ' "' . $r['attr5'] . ' ; ' . $r['val5'] . '"' . "<br />";
}
if($r['attr6'] === 0 || $r['attr6'] === ''){
echo '';
}else{
echo '"6"' . ' "' . $r['attr6'] . ' ; ' . $r['val6'] . '"' . "<br />";
}
if($r['attr7'] === 0 || $r['attr7'] === ''){
echo '';
}else{
echo '"7"' . ' "' . $r['attr7'] . ' ; ' . $r['val7'] . '"' . "<br />";
}
if($r['attr8'] === 0 || $r['attr8'] === ''){
echo '';
}else{
echo '"8"' . ' "' . $r['attr8'] . ' ; ' . $r['val8'] . '"' . "<br />";
}
if($r['attr9'] === 0 || $r['attr9'] === ''){
echo '';
}else{
echo '"9"' . ' "' . $r['attr9'] . ' ; ' . $r['val9'] . '"' . "<br />";
}
if($r['attr10'] === 0 || $r['attr10'] === ''){
echo '';
}else{
echo '"10"' . ' "' . $r['attr10'] . ' ; ' . $r['val10'] . '"' . "<br />";
}
if($r['attr11'] === 0 || $r['attr11'] === ''){
echo '';
}else{
echo '"11"' . ' "' . $r['attr11'] . ' ; ' . $r['val11'] . '"' . "<br />";
}
if($r['attr12'] === 0 || $r['attr12'] === ''){
echo '';
}else{
echo '"12"' . ' "' . $r['attr12'] . ' ; ' . $r['val12'] . '"' . "<br />";
}
if($r['attr13'] === 0 || $r['attr13'] === ''){
echo '';
}else{
echo '"13"' . ' "' . $r['attr13'] . ' ; ' . $r['val13'] . '"' . "<br />";
}
if($r['attr14'] === 0 || $r['attr14'] === ''){
echo '';
}else{
echo '"14"' . ' "' . $r['attr14'] . ' ; ' . $r['val14'] . '"' . "<br />";
}
if($r['attr15'] === 0 || $r['attr15'] === ''){
echo '';
}else{
echo '"15"' . ' "' . $r['attr15'] . ' ; ' . $r['val15'] . '"' . "<br />";
}
if($r['attr16'] === 0 || $r['attr16'] === ''){
echo '';
}else{
echo '"16"' . ' "' . $r['attr16'] . ' ; ' . $r['val16'] . '"' . "<br />";
}
echo "}";
echo "<br>";
}
}
echo "}";
echo "<br>";
}
echo "<p>" . '}' . "</p>";
What i'm trying to display the text as
custom_weapons_v3
{
STEAM_0:0:1621342
{
"0" //Bat
{
"level" "0"
"quality" "0"
"1" "23 ; 21"
"2" "231 ; 231"
"3" "231 ; 0"
}
"159" //Dalokohs Bar
{
"level" "4"
"quality" "2"
"1" "22 ; 32"
"2" "12 ; 42"
}
}
}
You can use file_put_contents(), but use PHP_EOL instead of <br />.
<?php
file_put_contents('test.html', "<html>". PHP_EOL ."<head><title>Test</title></head>". PHP_EOL ."<body>". PHP_EOL ."<h1>Hello World!</h1>". PHP_EOL ."</body>". PHP_EOL ."</html>");
?>
test.html will be,
<html>
<head><title>Test</title></head>
<body>
<h1>Hello World!</h1>
</body>
</html>
You can use the following code,
<?php
$data = "";
$data .= "custom_weapons_v3". PHP_EOL;
$data .= "{". PHP_EOL;
foreach($arr as $key => $a){
$data .= PHP_EOL
$check = $a['steamid'];
$data .= $check;
$data .= PHP_EOL . "{". PHP_EOL;
foreach($data as $key => $r){
-------------------
-------------------
}
}
$data .= "}" . PHP_EOL . "}";
file_put_contents('output.txt', $data);
?>

How to return a row count

I'm having trouble getting the row counts back from a MySQL query in PHP.
A link to the test page and the PHP code is as follows:
<?php
$con=* TESTED AND WORKING *
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$retDiv = "";
$counter = 0;
$result = mysqli_query($con,"SELECT * FROM `store` ORDER BY `num` DESC ");
$num_rows = mysql_num_rows($result);
while($row = mysqli_fetch_array($result))
{
if ($counter>=5)
{
break;
}
else
{
$retDiv = "<div id=\"hs" . $row['num'] . "\" class=\"store-item " . $row['Artist'] . "\">\n";
$retDiv = $retDiv . "<div id=\"". $row['Release'] . "-title\" class=\"album-title\">\n";
$retDiv = $retDiv . "<h1 align=\"center\" style=\"text-transform:uppercase;\">" . $row['Release'] . "</h1></br>\n";
$retDiv = $retDiv . "<h3 align=\"center\">" . $row['ReleaseName'] . "</h3>\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div id=\"" . $row['Release'] . "-description\" class=\"description\">\n";
$retDiv = $retDiv . "<table width=\"100%\">\n";
$retDiv = $retDiv . "<td width=\"50%\" style=\"padding-left:57px;padding-right:57px;padding-top:57px;padding-bottom:20px;\">\n";
$retDiv = $retDiv . "<a id=\"" . $row['Release'] . "DivLink\" href=\"javascript:;\" onmousedown=\"toggleDivStore('" . $row['Release'] . "-expand','" . $row['Release'] . "DivLink','" . $row['Release'] . "','" . $row['Release'] . "b','" . $row['Release'] . "')\"><img name=\"" . $row['Release'] . "-art\" src = \"images/" . $row['Release'] . ".jpg\" class=\"album-art\" onmouseover=\"this.src='images/" . $row['Release'] . "b.jpg'\" onmouseout=\"this.src='images/" . $row['Release'] . ".jpg'\"></a>\n";
$retDiv = $retDiv . "<td width=\"50%\" style=\"padding:20px;padding-top:0px;padding-bottom:0px;padding-left:0px;\">\n";
$retDiv = $retDiv . "<p style=\"\">" . $row['Description'] . "</p>\n";
$retDiv = $retDiv . "</td>\n";
$retDiv = $retDiv . "</table>\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div class=\"album-expand\" id=\"RELEASE-expand\" style=\"display:none;\">\n";
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "<div =\"" . $row['Release'] . "PaypalContainer\" class=\"album-paypal-container\">\n";
$retDiv = $retDiv . $row['Paypal1'];
$retDiv = $retDiv . "</div>\n";
$retDiv = $retDiv . "</div>\n";
echo $retDiv;
}
$counter = $counter+1;
}
echo "<div id=\"retNum\"><script type=\"text/javascript\">numReleases =" . $num_rows . ";numPages=Math.ceil(numReleases/5)-1;</SCRIPT></div>";
mysqli_close($con);
How to return the row count as $num_rows to the retNum Javascript?
You're mixing mysqli (note the i) and mysql (note the LACK of an i). The two libraries and function bodies are NOT interoperable:
$num_rows = mysql_num_rows($result);
^--- missing an `i` here
When you are using mysqli_query() method to get the result set from the database, the number of rows from the result can be obtained by calling mysqli_num_rows() method which takes the mysqli_result set as the argument and do not use mysql_num_rows(). Check mysqli_num_rows()
Instead of using PHP logic to break out, why not add "LIMIT 5" to your sql query?

PHP While loop, echo Issues

There has to be a easier way...
I keep getting this for the second line.
Parse error: syntax error, unexpected ';'
while($row = mysql_fetch_array($result)){
echo ("'$MAP_OBJECT->addMarkerByCoords";
"(\"";
$row['longitude'];
",";
$row['latitude'];
",\"";
$row['routername'];
"-";
$row['desc'];
"\", \"";
$row['routername'];
"-";
$row['desc'];
"<br><a href=\"./div/";
$row['routername'];
"\">Site Info</a>'");
echo "<br />";
}
You have to combine with a . and not with ;
echo ("'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
....
Have a look into the manual: http://php.net/manual/en/language.operators.string.php
Most of the ; should be . if you are attempting to concatenate these strings:
while($row = mysql_fetch_array($result)){
echo ("'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
"," .
$row['latitude'] .
",\"" .
$row['routername'] .
"-" .
$row['desc'] .
"\", \"" .
$row['routername'] .
"-" .
$row['desc'] .
"<br><a href=\"./Ldiv/" .
$row['routername'].
"\">Site Info</a>'"); // Here's the actual end of the statement
echo "<br />";
}
This woudl be a lot tidier with a HEREDOC:
echo <<<ROW
$MAP_OBJECT->addMarkerByCoords(
{$row['longitude']},
{$row['latitude']},
"{$row['routername']}-{$row['desc']}",
"$row['routername']}-{$row['desc']}"<br>
Site Info
)
ROW;
Although , it looks like something is missing before the <br> since the previous quote doesn't get closed.
Your syntax is wrong for what you are trying to execute.
The ; in PHP is an end statement, essentially. You are telling PHP to stop executing the echo on the very first line echo ("'$MAP_OBJECT->addMarkerByCoords" ; which is NOT what you want.
Instead, replace all the ; with .'s except for the last echo statement
while($row = mysql_fetch_array($result)){
echo "'$MAP_OBJECT->addMarkerByCoords" .
"(\"" .
$row['longitude'] .
"," .
$row['latitude'] .
",\"" .
$row['routername'] .
"-" .
$row['desc'] .
"\", \"" .
$row['routername'] .
"-" .
$row['desc'] .
"<br><a href=\"./Ldiv/" .
$row['routername']; .
"\">Site Info</a>'";
echo "<br />";
}

Categories