How can I generate html table form this url's json output?
http://megagrup.site/entegrasyon/hepsiburada.php
Update
I will find result like this;
<?php
$json=file_get_contents("url");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>".$stand->abc."</td>";
echo "<td>".$stand->def."</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
I will find result like this;
<?php
$json=file_get_contents("url");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo "<tr>";
echo "<td>".$stand->abc."</td>";
echo "<td>".$stand->def."</td>";
echo "</tr>";
}
// Close the table
echo "</table>";
}
?>
If I understand your question correctly you want to output the properties of the $stand object. You need an additional loop to iterate over the properties.
<?php
$json=file_get_contents("http://megagrup.site/entegrasyon/hepsiburada.php");
$data = json_decode($json);
if (count($data->listings)) {
// Open the table
echo "<table>\n";
// Cycle through the array
foreach ($data->listings as $idx => $stand) {
// Output a row
echo " <tr>\n";
// Output a cell for each property of the $stand object
foreach ($stand as $key => $value) {
echo " <td>" . $value . "</td>\n";
}
echo " </tr\n";
}
// Close the table
echo "</table>\n";
}
?>
Related
EDIT: Updated code, still not working
I'm trying to get tables to be displayed on html through a button click, unless it is possible to automatically load up tables on page initiation.
I keep running into an error: Fatal error: Uncaught Error: Call to undefined function mysql_fetch_array() on Line 15.
I am using a .php script
Here is my code:
<?php
include ("ConnDetails.php");
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn, "SELECT * FROM Tracks");
if ($result !== false) {
echo "<table border='1'>
<tr>
<th>Tracks<th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Tracks'] . "</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "You have encountered an error";
}
$conn->close();
?>
Thanks in advance
I think you can use:
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" . $row['Tracks'] . "</td>";
echo "</tr>";
}
echo "<table>";
} else {
echo "You have encountered an error";
}
$conn->close();
?>
Here is the table code - when this is printed out the has one empty row at the bottom. I am not sure why the last row is added? Is it perhaps the if else statement?
<?php
$row = mysql_fetch_array($result);
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Contact</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>Contact Us</TD>\n";
}
} else {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD
id=tblrowsHead2>Price</TD></TR>\n";
for ($i = 0; $i < $numofrows; $i++) {
$row = mysql_fetch_array($result); //get a row from our result set
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo "<TD id=Size>" . $row['Item'] . "</TD><TD id=Size>" . $row['Size']
. "</TD>
<TD id=Price>$" . $row['Price'] . "</TD>\n";
}
}
//now let's close the table and be done with it
echo "</TABLE>\n";
?>
Try this code instead:
<?php
$row = mysql_fetch_array($result);
$i = 0;
if ($row['Price'] == '0.00') {
echo "<TABLE width='100%'>\n";
echo "<TR><TD id=tblrowsHead2>Item</TD><TD id=tblrowsHead2>Size</TD><TD id=tblrowsHead2>Contact</TD></TR>\n";
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>Contact Us</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
} else {
do {
if ($i % 2) { //this means if there is a remainder
echo "<TR bgcolor=\"#E6E6FA\">\n";
} else { //if there isn't a remainder we will do the else
echo "<TR bgcolor=\"white\">\n";
}
echo '<TD id=Size>'.$row['Item'].'</TD><TD id=Size>'.$row['Size'].'</TD>';
echo "<TD id=Price>$" . $row['Price'] . "</TD></TR>\n";
$i++;
} while ($row = mysql_fetch_array($result)); //get a row from our result set
}
//now let's close the table and be done with it
echo "</TABLE>\n";
When you using the mysql_fetch_array at the first line of for-loop, you will lost the first record of your data-table, so you will have an empty line at the end
<?php
$conn = mysql_connect("localhost","root","");
if(!$conn){
echo mysql_error();
}
$db = mysql_select_db("imagestore",$conn);
if(!$db ){
echo mysql_error();
}
$q = "SELECT * FROM imagetable";
$r = mysql_query("$q",$conn);
if($r)
{
while($row=mysql_fetch_array($r) )
{
header("Content-type: text/html");
echo "</br>";
echo $row['photoname'];
echo "</br>";
$type = "Content-type: ".$row['phototype'];
header($type);
echo "<img src=image.php?fotoid=". $row['fotoid']."width =300 height = 35. 300/>";
}
}
else{
echo mysql_error();
}
?>
I know ORDER by but is not working to display photos as i want in my page .
I am a beginner .
I used $q = "SELECT * FROM imagetable ORDER BY fotoid DESC";
I've made a script for movies but i have an issue.
The home of script is contain code like this :
<div class="box">
<div id="movie"></div><div id="movie"></div><div id="movie"></div>
</div>
I've do a mysql query but i don't know how to echo just 3 record per div .
Im using this code for mysql query :
$query=mysql_query("select * from movies where id=$id");
while($row=mysql_fetch_assoc($query))
{
echo $row['name'];
}
You will need something like this I think
$query = mysql_query("select * from movies");
$result = array();
while($r = mysql_fetch_assoc($query)) {
$result[$r['id']] = array($r['name'],$r['thumb']);
}
$i = 0;
foreach($result as $id => $data){
if($i == 0)
{
echo "<div class=\"box\">";
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 1)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
$i = $i + 1;
}
elseif($i == 2)
{
echo "<div id=\"movie\">";
echo "ID: $id";
echo "Name: $data[0]";
echo "Thumb: $data[1]";
echo "</div>";
echo "</div>";
$i = 0;
}
}
Multiple elements with the same id? Not a good idea.
You didn't say what you want to happen if you're query returns less then 3 rows.
You might try:
...
$x=0;
while($row=mysql_fetch_assoc($query) && ++$x<=3) {
echo $row['name'];
}
This is my script:
<?php
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);
backup_tables('localhost','xxxxx','xxxxx','xxxxx');
/* backup the db OR just a table */
function backup_tables($host,$user,$pass,$name,$tables = '*')
{
$link = mysql_connect($host,$user,$pass);
mysql_select_db($name,$link);
//get all of the tables
if($tables == '*')
{
$tables = array();
$result = mysql_query('SHOW TABLES');
while($row = mysql_fetch_row($result))
{
$tables[] = $row[0];
}
}
else
{
$tables = is_array($tables) ? $tables : explode(',',$tables);
}
//cycle through
foreach($tables as $table)
{
$result = mysql_query('SELECT * FROM '.$table);
$num_fields = mysql_num_fields($result);
$return.= 'DROP TABLE '.$table.';';
$row2 = mysql_fetch_row(mysql_query('SHOW CREATE TABLE '.$table));
$return.= "\n\n".$row2[1].";\n\n";
for ($i = 0; $i < $num_fields; $i++)
{
while($row = mysql_fetch_row($result))
{
$return.= 'INSERT INTO '.$table.' VALUES(';
for($j=0; $j<$num_fields; $j++)
{
$row[$j] = addslashes($row[$j]);
$row[$j] = ereg_replace("\n","\\n",$row[$j]);
if (isset($row[$j])) { $return.= '"'.$row[$j].'"' ; } else { $return.= '""'; }
if ($j<($num_fields-1)) { $return.= ','; }
}
$return.= ");\n";
}
}
$return.="\n\n\n";
}
$backup_file = $_SERVER['DOCUMENT_ROOT'] . '/scripts_and_crons/fr_internetlead-'.time().'.sql';
//save file
$handle = fopen($backup_file,'w+');
fwrite($handle,$return);
fclose($handle);
}
//define the receiver of the email
$to = 'email#domain.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: backups#domain.com\r\nReply-To: noreply#domain.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks
$attachment = chunk_split(base64_encode(file_get_contents($backup_file)));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: multipart/alternative; boundary="PHP-alt-<?php echo $random_hash; ?>"
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
Hello World!!!
This is simple text email message.
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<h2>Hello World!</h2>
<p>This is something with <b>HTML</b> formatting.</p>
--PHP-alt-<?php echo $random_hash; ?>--
--PHP-mixed-<?php echo $random_hash; ?>
Content-Type: application/sql; name="<?php echo $backup_file; ?>"
Content-Transfer-Encoding: base64
Content-Disposition: attachment
<?php echo $attachment; ?>
--PHP-mixed-<?php echo $random_hash; ?>--
<?php
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = #mail($to, $subject, $message, $headers);
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed"
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
The only debugging info I get in php are
Notice: Undefined variable: return in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 38
and
Deprecated: Function ereg_replace() is deprecated in /var/www/vhosts/domain.com/httpdocs/scripts_and_crons/internetlead_backup.php on line 50 x infinity
The only attachment I receive in the email is ATT00001.
echo '<pre>';
echo $return;
echo '</pre>';
Prints out the correct data.
Even doing
echo '<pre>';
echo file_get_contents($backup_file);
echo '</pre>';
Prints out the correct data so the file is being written to but for some reason the script fails at attaching it to the email.
Can't see what could be wrong, any ideas?
Martin
UPDATE
Found the answer in another SO question - PHP mail() attachment problems
That is the correct way to do it.
If your php version is higher than 5.2 you should replace ereg_replace with preg_match and change (“ parts as (“/