itunes search store api - json

I want to get data from the API iTunes Store to my PHP file. In the search form you can fill the artist and title of the song. I want to show only the data from the song. This is my code:
$title = $_POST["title"];
$artist = $_POST["artist"];
$query_string = urlencode($title);
$query_string2 = urlencode($artist);
$json = file_get_contents('https://itunes.apple.com/search?term=$query_string&term=$query_string2'); // this WILL do an http request for you
$data = json_decode($json);
echo "<div class='container' id='movies'>";
echo "<div class='col-md-4' id='movie'>";
echo "<img src='" .$data->artworkUrl60. "' width='200'>";
echo "<h4>" .$data->artistName. " - " .$data->trackName. "</h4>";
i get this error: Notice: Undefined property: stdClass::$artworkUrl60 in....
Whats wrong with my code?

Because your $data Object doesn't contain an attribute named $artworkUrl60.
Your HTTP query doesn't not work correctly, you should use double quote instead of single quote.
// For limit you can add 'limit' parameter
$json = file_get_contents("https://itunes.apple.com/search?term=$query_string&term=$query_string2&limit=20");
// OR concatenation
// $json = file_get_contents('https://itunes.apple.com/search?term='.$query_string.'&term='.$query_string2);
$data = json_decode($json);
if (0 == $data->resultCount) {
echo 'No result found ! ';
}
else {
foreach ($data->results as $row){
echo "<div class='container' id='movies'>";
echo "<div class='col-md-4' id='movie'>";
echo "<img src='" .$row->artworkUrl60. "' width='200'>";
echo "<h4>" .$row->artistName. " - " .$row->trackName. "</h4>";
}
}
https://affiliate.itunes.apple.com/resources/documentation/itunes-store-web-service-search-api/#searchexamples

Related

Getting data from database using part of the URL as a value - PHP

My url type is like "http://localhost/boardgame/profile/Duncan/" the last word is the user name...
I just try to get some data from database and I need a user name to do this. I tried to get it from page URL but it doesn't work. Where did I go wrong? What has to be done? I'll be greateful if anybody can help...
<?php
$actual_link = 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
//echo $actual_link;
$rest = substr($actual_link, 35, -1);
echo $rest;
$link = mysqli_connect("localhost", "root", "", "bgt");
if (mysqli_connect_error()) {
die ("Veritabanı bağlantısında hata var");
} if (($rest) !="") {
if (get_current_user_id($link) == '0') {
echo "<p>Kullanıcının koleksiyonunu görebilmek için giriş yapmanız
gerekiyor.</p>";
} else {
$query = "SELECT TITLE FROM wp_user_collections WHERE `user_name` =
'".mysqli_real_escape_string($link, $rest)."'";
echo "<div><h1>'".mysqli_real_escape_string($link, $rest)."'in
Koleksiyonu</h1><div>";
if($result = mysqli_query($link, $query)){
if(mysqli_num_rows($result) > 0){
echo "<table>";
echo "<tr>";
echo "<th>Oyun Adı</th>";
echo "</tr>";
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td>'" . $row['TITLE'] . "'</td>";
echo "</tr>";
}
echo "</table></div>";
}
}
}
} else {
echo "Kullanıcının koleksiyonunu görebilmek için <a
href='http://localhost/boardgame/profile/'".mysqli_real_escape_string($link,
$rest)."'>tıklayınız</a>";
}
?>
Here is the solution I have thought...using array then extracting the last element (or second last since there is a trailing '/' slash which is a delimiter)
$uri="http://localhost/boardgame/profile/Duncan/";
$uriParts=explode('/', $uri); //converts the uri string into an array of words separated by forward slash
$name=$uriParts[sizeOf($uriParts)-1-1]; //the user name comes from 2nd last item as the last item is empty
echo $name;
I could suggest you use Regular Expression to extract the user name, however I'm not well versed with it yet.

Read txt file into HTML table

I have to read a txt file into a HTML table.
There are many fields in it but I only want to read the field that says "value".
Here is my txt file:
one=availability:, timestamp=90754, value=no
two=description:, timestamp=074693, value=not sure
three=Name, timestamp=90761, value=yes
The one, two, three values are my row headers and I want it to display the values underneath.
Is there anyway of doing this using iframe?? PHP is not working for me.
Supposing the value is always the last field and you are reading the file line by line I would use a dirty approach:
$value = explode('value=', $line)[1];
longwinded approach
$headers=[];
$values=[];
$lines = file('./homework.txt', FILE_IGNORE_NEW_LINES);
foreach($lines as $line){
$chunks = explode(',',$line);
foreach($chunks as $index => $chunk){
list($key, $value) = explode('=', trim($chunk));
if(!$index){
$headers[] = $value;
}
if('value' === $key){
$values[] = $value;
}
}
}
echo "<table><thead><tr>";
foreach($headers as $h){
echo "<th>{$h}</th>";
}
echo "</tr></thead><tbody><tr>";
foreach($values as $v){
echo "<td>{$v}</td>";
}
echo "</tr></tbody></table>";
If all rows follow the same pattern you can probably use:
//$textfilestring = "one=availability:, timestamp=90754, value=no
//two=description:, timestamp=074693, value=not sure
//three=Name, timestamp=90761, value=yes";
$textfilestring = file_get_contents("PATH_TO_FILE");
$arraylines = explode("\n", $textfilestring);
for ($i=0;$i<count($arraylines);$i++) {
$arraylines[$i] = explode("value=", $arraylines[$i]);
}
echo "<pre>";
var_dump($arraylines);
echo "</pre>";
echo $arraylines[0][1] . "<br>";
echo $arraylines[1][1] . "<br>";
echo $arraylines[2][1] . "<br>";
$arraylines should be twodimensional with one part beeing
one=availability:, timestamp=90754,
and one beeing
no
Not tested though.

How can I prevent them from inserting their answer/post ? (And how to add an error message?)

I'm using this code for my questionnaire, how can I prevent them from inserting their answers/post? If they did not yet clicked all the radio buttons, that is given within the system which is the questions? And how to add an error message besides the question?
if(isset($_POST['question']))
{
$AddQuery = "INSERT INTO tblevaluate (evaluateid,professorid,professorname,studentid,course,section,subjectid,subjectname) VALUES ('','$server_professorid','$server_name',' $username','$course','$section','$server_subjectid','$server_subject')";
mysql_query($AddQuery, $connect);
$id = mysql_insert_id();
foreach($_POST['question'] as $questionId => $answer)
{
$AddQuery = "INSERT INTO tblanswer (answervalue,evaluateid,professorid,professorname,studentid,course,section,subjectid,subjectname) VALUES ($answer,$id,$server_professorid,'$server_name',$username,'$course','$section',$server_subjectid,'$server_subject')";
mysql_query($AddQuery, $connect);
header('Location: evaluate.php');
}
}
while($row = mysql_fetch_array($result))
{
echo "<br>";
echo "<strong>" . $row["questionno"] . ".</strong> " . $row["question"] . "";
echo "<br>";
echo "<input type = radio name = 'question[". $row ["questionid"] . "]' value = '5'/><label>5</label>";
echo "<input type = radio name = 'question[". $row ["questionid"] . "]' value = '4'/><label>4</label>";
echo "<input type = radio name = 'question[". $row ["questionid"] . "]' value = '3'/><label>3</label>";
echo "<input type = radio name = 'question[". $row ["questionid"] . "]' value = '2'/><label>2</label>";
echo "<input type = radio name = 'question[". $row ["questionid"] . "]' value = '1'/><label>1</label>";
echo "<br>";
echo "<hr size = 5 color = black noshade >";
}
The Answer came from user2864740
Make the radio groups required is the simplest way to prevent form submission. To be 'truly safe' you'd also need to ensure that all the values are set to allowable values on the server, but meh - not really any point even talking about that when there is Blatant SQL Injection vulnerabilities. Also, your tables are poorly normalized. – user2864740

How to use IF NULL, do this in MySQL?

I'm trying to build a members-system in which you can view certain values from a database.
It worked, so that was great, but I'm having a problem now because I'm using a NULL value on 'age'.
When viewing a profile page, it looks like this:
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$row = mysql_fetch_array($result);
echo "<b>" . $row['userusername'] . "</b>: </p>"; ?>
<?php
$id = $_GET["id"];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id]");
$noage = mysql_query("SELECT ISNULL([age]) FROM membersfromsite WHERE `idofmember`=$_GET[id]");
while ($row = mysql_fetch_array($result))
{
echo "<p class='middle'>id-number: " . $row['idofmember'] . "<br>";
echo "Username: " . $row['userusername'] . "<br>";
if ($noage)
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
}
I have tried all kinds of other things, but the problem which I 'm having is that it either returns 'Age not specified' on every userpage or the age on every userpage, including the pages with a NULL value, which makes it look like:
Age:
The code which you can see above returns the age on every page, including the pages with an age which is set to NULL. What I don't understand is if I change the code to this:
$noage = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember`=$_GET[id] AND age IS NULL");
it simply doesn't work. Since I'm using IS NULL instead of = NULL I don't really see why this shouldn't work, but I guess it has to do with the IF which is inside the 'while' thing, I don't really see in what other way I could fix this though...
I'm having an idea what the problem is, because I think that there is already a MyQS, Query done with Results and $noage is maybe ignored because of this, but I don't know how to solve this.
You don't need to do a whole separate $noage query.
Just do:
if(!$row['age'])
{
echo "Age not specified";
}
else
{
echo "Age: " .$row['age'] ;
}
Instead of if($noage) use if(!$row['age']) and skip the second query.
The other reason your code does not work is that the second query returns an array with something like array('expr1' => 0) which is not false. You only get a false result if nothing is found.
The reason why = NULL does not work? There are books written about it, it is just as it is.
Rather than relying on MySQL to tell us if no age was found or not, we can programatically determine whether the age value is Null right in PHP.
Here is an example:
$id = $_GET['id'];
$result = mysql_query("SELECT * FROM membersfromsite WHERE `idofmember` = '" . mysql_real_escape_string($id) . "'");
while($row = mysql_fetch_array($result)) {
echo '<p class='middle'>id-number: ' . $row['idofmember'] . '<br>';
echo 'Username: ' . $row['userusername'] . '<br>';
if($row['age'] === Null) {
echo "Age not specified";
} else {
echo "Age: " .$row['age'] ;
}
}

htmlspecialchars protection isn't working

I made a function to print all the comments from a id-page. But I want to protect my site from being hacked with htmlspecialchars. So I put them arround my post that will be printed. The problem is that it isn't working? I can do whatever I want with the <>-signs.
CODE FUNCTION
public function GetAllComments($id)
{
$db = new Db();
$select = "SELECT * FROM tblcomments WHERE bug_id =" . $id . " ORDER BY comment_id DESC";
$result = $db->conn->query($select);
while($row = mysqli_fetch_assoc($result))
{
echo "<li class='description'>" . htmlspecialchars($row["comment_text"]) . "</li>";
echo "<li class='user'>" . $row['name'] . "</li>";
}
}
CODE PRINTING
if(isset($bugs)){
foreach ($bugs as $bug) {
echo " " .
$bug['bug_title'] . "" .
$bug['bug_status'] . "From:
" . $bug['name'] . " To: " .
$bug['bug_to'] . " Project: " .
$bug['project_title'] . "";
}
}
depends on what you want to save from you should use htmlspecialchars like this
htmlspecialchars(trim($variable), ENT_QUOTES);