soundcloud http request search engine - php

I want to retrieve XML data from soundcloud using a simple search engine, that retrieves results in a simple results table array
$tracks = "https://api.soundcloud.com/tracks?q=".$var."&client_id=bbba84be29098bdaad6a5de1c048e3e9&limit=10";
$mysongs = simplexml_load_file($tracks);
echo "<table>";
echo '<table cellpadding="0" cellspacing="0">';
echo '<tbody>';
echo '<tr>';
echo '<th>State</th> <th>Name</th><th>Song</th><th>artist</th><th>user</th> <th>user</th>';
echo '</tr>';
echo '</tbody>';
foreach ($mysongs->track->user as $track) {
echo '<tbody>';
echo '<tr>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo " ";
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo " ";
echo '</td>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo '<td>';
echo $user->kind;
echo '</td>';
echo '</tr>';
echo '</tbody>';
}
this is my code but nothing seems to display as a result, i have tested the same format of code on another api (yelp) and it does work in xml, so what am i missing here? i dont want to use json, i need it to be xml ( the user -> kind ) is just for testing the code right now.

the variable $user doesn't exist. the variable you want is $track. so where you have
echo $user->kind
replace that with
echo $track->kind
you can see the format of the returned object (and it's hierarchy by running the code in the cli and
print_r($mysongs)

Related

PHP: List multiple record grouped by date

I'm trying to list multiple record from MySQL Database using PHP but when grouping it returns only one record from database, below is my PHP code I'm using.
<?php
include "connection.php";
$query = "select * from lectureupload GROUP BY submittedOn";
$result=mysqli_query($conn, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$row['submittedOn'].'</h3>';
echo '</div>';
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
echo '</table>';
}
mysqli_close($conn);
?>
The current Result it gives me is somewhat like that,
My Database table is this.
Solution to this problem will be highly appreciated.
Regards.
You can't use GROUP BY clause to retrieve all records from that table. GROUP BY will always return one row per GROUP BY CONDITIONAL_COLUMN. I will suggest you below modification:
<?php
include "connection.php";
//ORDER BY submittedOn will make sure the records retrieved in ordered as per submittedOn.
$query = "select * from lectureupload ORDER BY submittedOn";
$result = mysqli_query($conn, $query);
$submitted_on_keys = array();
while ($row = mysqli_fetch_array($result)) {
if (!in_array($row['submittedOn'], $submitted_on_keys)) {
if (count($submitted_on_keys) > 0) {
//It means we have already created <table> which needs to be closed.
echo '</table>';
}
$submitted_on_keys[] = $row['submittedOn'];
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>' . $row['submittedOn'] . '</h3>';
echo '</div>';
}
echo '<tr >';
echo '<td>' . $row['id'] . '</td>';
echo '<td>' . $row['title'] . '</td>';
echo '<td>' . $row['semester'] . '</td>';
echo '<td>' . $row['teacherName'] . '</td>';
echo '<td>' . $row['lectureLink'] . '</td>';
echo '<td>' . $row['submittedOn'] . '</td>';
echo '</tr>';
}
if (count($submitted_on_keys) > 0) {
//There is last <table> which needs to be closed.
echo '</table>';
}
mysqli_close($conn);
?>
You should group it in your PHP code rather than your SQL.
Something like this:
$lectures = [];
$result = mysqli_query($conn, "select * from lectureupload");
while ($row = mysqli_fetch_array($result)) {
$lectures[$row['submittedOn']][] = $row;
}
foreach ($lectures as $submittedOn => $rows) {
echo '<table class="table">';
echo '<thead>';
echo '<td><strong>ID</strong></td>';
echo '<td><strong>Title</strong></td>';
echo '<td><strong>Semester</strong></td>';
echo '<td><strong>Teacher</strong></td>';
echo '<td><strong>Lecture Link</strong></td>';
echo '<td><strong>Submitted On</strong></td>';
echo '</thead>';
echo '<div class="section-header">';
echo '<br>';
echo '<h3>'.$submittedOn.'</h3>';
echo '</div>';
foreach ($rows as $row) {
echo '<tr >';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['title'].'</td>';
echo '<td>'.$row['semester'].'</td>';
echo '<td>'.$row['teacherName'].'</td>';
echo '<td>'.$row['lectureLink'].'</td>';
echo '<td>'.$row['submittedOn'].'</td>';
echo '</tr>';
}
echo '</table>';
}

ID'd link on click

I'm creating a contact based system, i have a contact list and want it to go to a full contact page when i click on a button, but the variable is being detected as a function?
<div id="po2" style="Margin:10% ">
<h1>Contacts</h1>
<?php
$sql = "SELECT * FROM `contacts`";
$query = mysqli_query($conn, $sql);
echo '<table class="data-table">';
echo'<thead>';
echo'<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo'</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
echo '<tr>';
echo '<td>'.$row['Forename'].'</td>';
echo '<td>'.$row['Surname'].'</td>';
echo $var==$row['Forename']("<td><a href='View.php?ID= " . urlencode($var) ."'>
<button type='button'>link</button>
</a></td>");
echo '</tr>';
}
echo'</tbody>';
echo '</table>';
?>
</div>
You are using a comparison of $var and the $row. Try setting $var to the $row each loop iteration.
echo '<thead>';
echo '<tr>';
echo '<th>Forename</th>';
echo '<th>Surname</th>';
echo '<th>Other</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';
while ($row = mysqli_fetch_array($query))
{
$var = $row['Forename'];
echo '<tr>';
echo '<td>' . $var . '</td>';
echo '<td>' . $row['Surname'] . '</td>';
echo "<td><a href='View.php?ID=" . urlencode($var) . "'><button type='button'>link</button></a></td>";
echo '</tr>';
}
echo '</tbody>';

Enumerate table rows in html

I have this code which prints a table taken from a search query and I want to enumerate them using PHP. I'm trying a for loop but unsuccessfully. Can anyone please help me?
<table id="students">
<?php
$iterator = new CDataProviderIterator($dataProvider);
//echo count($iterator);
echo '<thead>';
echo '<tr>';
echo '<th>'.Yii::t('default', 'Last Name').'</th>';
echo '<th>'.Yii::t('default', 'First Name').'</th>';
echo '<th>'.Yii::t('default', 'Description').'</th>';
echo '<th>'.Yii::t('default', 'Debit').'</th>';
echo '<th>'.Yii::t('default', 'Credit').'</th>';
echo '<th>'.Yii::t('default', 'Date').'</th>';
echo '<th>'.Yii::t('default', 'Trans number').'</th>';
echo '</tr>' ;
echo '</thead>';
echo '<tfoot>';
echo '<tr>';
echo '<td>'.Yii::t('default', 'Page Total').'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '</tr>';
echo '</tfoot>';
$sumdebit=0;
$sumcredit=0;
foreach($iterator as $rec) {
$sumdebit+=$rec['debit'];
$sumcredit+=$rec['credit'];
echo '<tbody>';
echo '<tr>';
$count=0;
for ($i=0;$i<=40;$i++){
$count+=$i;
echo '<td>'.$count; '</td>';
echo '<td>'.$rec['student']['lastname']; '</td>';
echo '<td>'.$rec['student']['firstname'];'</td>';
echo '<td>'.$rec['transtype']['description']; '</td>';
echo '<td align="right">'.$rec['debit']; '</td>';
echo '<td align="right">'.$rec['credit']; '</td>';
echo '<td>'.$rec['transdate']; '</td>';
echo '<td>'.$rec['transnumber']; '</td>';
}
echo '</tr>';
echo '</tbody>';
}
echo '<tr>';
echo '<td>'.Yii::t('default', 'Total').'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '<td align="right">'.$sumdebit;'</td>';
echo '<td align="right">'.$sumcredit;'</td>';
echo '<td>'."".'</td>';
echo '<td>'."".'</td>';
echo '</tr>';
?>
</table>
My problem is that I want to enumerate the table rows, also I'm not very familiar with the syntax and all of this. I'm trying to make it work in Yii. which I'm not very familiar also. So the problem is I want to put numbers in front of every row. For example I have 20 table rows and I want to start counting and enumerate them. e.g. 1. George Cat 2. Tom Dog 3. John Bird etc..
Your question is unclear, you do not state what your actual problem is.
Therefore we can only guess...
[...]
echo '<tbody>';
echo '<tr>';
for ($i=0;$i<=40;$i++) {
echo '<td>'.$i.'</td>';
[...]
You probably received a syntax error which you apparently either didn't see (look into the error log files!) or simply ignored. At least you did not post it here though it probably exactly points out what the issue is...

Table problems in PHP

This is my current code (and image showing output). I want the table to be aligned and the Names of the values keep repeating. How can I achieve this?
<?php
$country['ph'] = array('Philippines', 'Manila', '+8');
$country['ru'] = array('Russia', 'Moscow', '-4');
$ctr = 1;
foreach ($country as $key => $value) {
echo '<table border = "1" >';
echo '<td>No';
echo '<td>Flag';
echo '<td>Code';
echo '<td>Name';
echo '<td>Capital';
echo '<td>Time Zone';
echo '<tr>';
echo '<td>', $ctr++, '</td>';
echo "<td><img src = \"/csnclass/img/flags/$key.png\"/></td>";
echo "<td>$key</td>";
echo '<td>',$value[0], '</td>';
echo '<td>',$value[1], '</td>';
echo '<td>',$value[2], '</td>';
echo '</tr>';
echo '</table>';
}
?>
Presently, your foreach is outputting a <table> for each item it iterates through - so the table cells in these tables won't be lined up, as cells in different tables have no relation to each other. Similarly, you're outputting the header of each table column for every single item as well.
A quick way to fix this is to take some of the echo calls outside of the foreach loop (mainly relating to the table and headers):
// Start table and print out headers
echo '<table border = "1" >';
echo '<tr>';
echo '<td>No';
echo '<td>Flag';
echo '<td>Code';
echo '<td>Name';
echo '<td>Capital';
echo '<td>Time Zone';
echo '</tr>';
// Print data for each country
foreach ($country as $key => $value) {
echo '<tr>';
echo '<td>', $ctr++, '</td>';
echo "<td><img src = \"/csnclass/img/flags/$key.png\"/></td>";
echo "<td>$key</td>";
echo '<td>',$value[0], '</td>';
echo '<td>',$value[1], '</td>';
echo '<td>',$value[2], '</td>';
echo '</tr>';
}
// End table
echo '</table>';
Hope this helps! Let me know if you have any questions.

Dynamically Apend data in html table from mysql using php

I have mysql table with following columns :
no, name, oname, quantity
I have n number of records in the tables ,I want to fetch these records in an html table that dynamically increases'decreases its row length according to the number of rows in mysql database. I am trying following but its not working.can anyone help me out here
<?php
include './connection.php';
$query = "select * from orderdetails";
$result = mysql_query($query);
echo '<table border="1" style="width:600px" align=center >';
echo '<tr bgcolor="lavendar">';
echo '<td width="15%">Order No.</td>';
echo '<td>Name</td>';
echo '<td>Order</td>';
echo '<td>Quantity</td>';
echo '</tr>';
echo '</table>';
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
echo '</table>';
?>
Where you are printing out your values, there are a few errors.
In this specific code block:
while( $row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' row['no'] '</td>';
echo '<td>' row['name'] '</td>';
echo '<td>' row['oname'] '</td>';
echo '<td>' row['quantity'] '</td>';
echo '</tr>';
}
First off all, you would want to concatinate your strings, PHP uses . for that.
For example, to concatinate "World!" to "Hello, ", to print out Hello, World!, you would use the following:
echo "Hello, " . "World!";
You are also missing a variable sign ($) in your code, when printing out the rows.
In your case, you're using echo '<td>' row['no'] '</td>';,
where it should have been echo '<td>' . $row['no'] . '</td>';
To clarify, your code should look like this:
while($row = mysql_fetch_assoc($result)){
echo '<tr>';
echo '<td>' . $row['no'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['oname'] . '</td>';
echo '<td>' . $row['quantity'] . '</td>';
echo '</tr>';
}
Relevant PHP documentation:
PHP String Concatination
Echo documentation
There's a typo-
row['no']
to
$row['no']
Similarly other variables too.

Categories