Vb.net programmer moving to PHP (Need help making a script)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
JJMPSP
Forum Newbie
Posts: 1
Joined: Thu Mar 03, 2011 12:35 pm

Vb.net programmer moving to PHP (Need help making a script)

Post by JJMPSP »

Hey there! I'm new here as you can see :)
I'm 17 and from the UK.

I've been programming in VB.net for almost a year now and am pretty comfortable with it :)
I have now decided to move forward and code with PHP and Javascript and thought I would join this forum and ask for a bit of help from you masters,if any of you people would be kind enough to provide it that is :)

So far from spending the last week browsing through random scripts found on the internet, I have learned quite a bit.

So far I have learned:

- That each line of code (well some exclusions,not every line), needs to have a semi colon at the end of it ";"
- Variables can be of ANY datatype in PHP (even an array) - just by typing "$variable_boolean = true;" - so theres no need to declare the variable as a datatype.
- PHP can be used in many ways, mixed in html of course, or with databases etc.
- THAT I NEED TO ACTUALLY REMEMBER THE NAME OF SOME FUNCTIONS IF I WANT TO START CODING WITH PHP.

Anyway, on to the coding part of this topic. So far I have created this (I know its mainly JS code right now, but ah well lol)

What I want to do is read an xml file and show it to the user in a friendly format. Here's my code:

Code: Select all

<?
    $playlisturl = $_GET['playlist'];

?>


<html>
<head>
<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET", "<?= $playlisturl ?>",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
x=xmlDoc.getElementsByTagName("song");

function displayCDInfo(i)
{
songname=(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
songid=(x[i].getElementsByTagName("id")[0].childNodes[0].nodeValue);
genre=(x[i].getElementsByTagName("genre")[0].childNodes[0].nodeValue);
txt='<img src="tune.jpg" alt="Image" border="0">'+" Song Name: "+songname+'<br /> <img src="tune.jpg" alt="Image" border="0">'+" Song ID: "+songid+'<br /> <img src="tune.jpg" alt="Image" border="0">'+" Song genre: "+genre+'<br /> <img src="http://img.youtube.com/vi/' +songid+ '/0.jpg" alt="Image" border="0">' ;
document.getElementById("songinfo").innerHTML=txt;
}
</script>
</head>

<body>
<div id='songinfo'>Click on a Song to view more information about it...</div><br />

<script type="text/javascript">
document.write("<table border='1'>");
for (var i=0;i<x.length;i++)
  { 
  document.write("<tr onclick='displayCDInfo(" + i + ")'>");
  document.write("<td>");
  document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
document.write("</table>");
</script>

</body>
</html>
My XML (playlist) structure:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<!--Tune Nanny Playlist - test playlist-->
<playlist>
  <info>
    <playlistname>test playlist</playlistname>
    <created>01/03/2011 18:51:01</created>
  </info>
  <song>
    <name>Alien Ant Farm - Smooth Criminal</name>
    <id>CDl9ZMfj6aE</id>
    <genre>0</genre>
  </song>
  <song>
    <name>Cream - Sunshine Of Your Love</name>
    <id>Cqh54rSzheg</id>
    <genre>0</genre>
  </song>
  <song>
    <name>Tom Jones - Mama Told Me Not To Come</name>
    <id>vpsu0n64bpQ</id>
    <genre>0</genre>
    </song>
</playlist>
As you can see i am getting a parameter after the url and displaying a filename using this method, is this safe ?
And how could I make the script more advanced to show a message if no url is passed to the php file ?


Thanks for reading, and I hope you can help :)
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Vb.net programmer moving to PHP (Need help making a scri

Post by AbraCadaver »

First off, in PHP it's not safe to trust user supplied data (from get, post etc), however, if you are only using this in the javascript then I personally don't see an issue with your code. If a valid URL to an XML file is provided then fine, if not then the javascript will error out.

I haven't read through all of the code, but a lot of the XML processing and other things can be done in PHP.

Don't use <? or <?=. Those are called short tags and are not enabled on all servers. Actually, in PHP 5.x they are disabled by default. Also, you can't use them in XML or XHTML pages either.

PHP does throw notices if you attempt to use vars that aren't defined, so check unless you have specifically defined them yourself:

Code: Select all

if(isset($_GET['playlist']) {
   $playlisturl = $_GET['playlist'];
} else {
   //do something else
}
Or cool ternary:

Code: Select all

$playlisturl = isset($_GET['playlist']) ? $_GET['playlist'] : /* something else */ ;
PHP is syntactically very similar to C, even C#
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Post Reply