i want to create dynamic banner rotater wih php ajax i want to pass the mysql_fetch_array() to an array to create a new array() to create xml response..........
here is my code
$sql = mysql_query("SELECT * FROM ads");
header('Content-type: text/xml');
echo '<?xml version="1.0" ?>';
while($row = mysql_fetch_array($sql)){
$title = $row['title'];
$img = $row['file'];
$body = $row['body'];
$ban = '<b>'.$title.'</b><br><br><img src="ads/'.$img.'"><br><br>'.$body;
$banners = array(
$ban,
);
$html = $banners[array_rand($banners)];
}
<banner>
<content><?php echo htmlentities($html); ?></content>
<reload>3000</reload>
</banner>
but it is return only one ad not return multiple ads how can i fix that
The proble is here: $banners = array($ban);. What you're trying to do is include all the ads in the $banners array as an entry but you are failing to achieve that.
The correct code for including an entry in an array would be $banner[] = $ban. That way each ad that comes as a result from your query will be stored as an individual entry.
So the correct code would be:
$sql = mysql_query("SELECT * FROM ads");
$banner = array(); //Define the array before trying to add elements.
header('Content-type: text/xml');
while($row = mysql_fetch_array($sql))
{
$title = $row['title'];
$img = $row['file'];
$body = $row['body'];
$ban = '<b>'.$title.'</b><br><br><img src="ads/'.$img.'"><br><br>'.$body;
$banner[] = $ban; //Adding a new entry at the end.
$html = array_rand($banner); //Getting a random entry.
}
Use this in the while loop:
$banners[] = $ban
Instead of
$banners = array(
$ban,
);
and
$html = array_rand($banners);
instead of
$html = $banners[array_rand($banners)];
And to what bzabhi said, define your
$banners = array();
before the while loop, and the randomization part has to go after the loop.
Related
I would like my CDATA section display the result of a function that generates a text from an sql query.
Create sql query and call function createXMLfile:
$statement = $connection->query("SELECT * FROM events ORDER BY ID DESC LIMIT 1");
$statement->execute();
$eventArray = array();
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
array_push($eventArray, $row);
}
createXMLfile($eventArray);
Function that generate text from my sql query:
<?php
function write_records($eventArray) {
for($i=0; $i<count($eventArray); $i++){
$name = $eventArray[$i]['event_name'];
$city = $eventArray[$i]['event_city'];
?>A new event <?php echo $eventArray[$i]['event_name'];?> is coming in <?php $eventArray[$i]['event_city'];?>.<?php
}
return $eventArray;
}
?>
Function that create XML file (i use implode for convert array to a string):
function createXMLfile($eventArray){
$filePath = 'event.xml';
$content = implode(' ', write_records($eventArray));
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('INCIDENTS');
for($i=0; $i<count($eventArray); $i++){
$eventName = $eventArray[$i]['event_name'];
$eventCity = $eventArray[$i]['event_city'];
$eventXml = $dom->createElement('INCIDENT');
$name = $dom->createElement('EVENT', $eventName);
$eventXml->appendChild($name);
$city = $dom->createElement('CITY', $eventCity);
$eventXml->appendChild($city);
$eventXml->appendChild(new DOMElement('COMMENTAIRE'))->appendChild(new DOMCdataSection($content));
$root->appendChild($eventXml);
}
$dom->appendChild($root);
$dom->save($filePath);
}
XML file is generated but CDATA section display only this :
<![CDATA[Array]]>
Please can you tell me what's wrong ? Thanks for your help.
EDIT:
function createXMLfile($eventArray){
$filePath = 'event.xml';
$content = implode(' ', write_records($eventArray[0]));
$dom = new DOMDocument('1.0', 'utf-8');
$root = $dom->createElement('INCIDENTS');
for($i=0; $i<count($eventArray); $i++){
$eventName = $eventArray[$i]['event_name'];
$eventCity = $eventArray[$i]['event_city'];
$eventXml = $dom->createElement('INCIDENT');
$name = $dom->createElement('EVENT', $eventName);
$eventXml->appendChild($name);
$city = $dom->createElement('CITY', $eventCity);
$eventXml->appendChild($city);
$eventXml->appendChild(new DOMElement('COMMENTAIRE'))->appendChild(new DOMCdataSection($content));
$root->appendChild($eventXml);
}
$dom->appendChild($root);
$dom->save($filePath);
}
Now sql values are displayed in CDATA section but not the generated text from function write_records($eventArray).
<![CDATA[concert Miami]]>
And i would like :
<![CDATA[A new concert is coming in Miami]]>
print_r($eventArray); give me same result :
$statement = $connection->query("SELECT * FROM events ORDER BY ID DESC LIMIT 1");
$statement->execute();
$eventArray = array();
while($row = $statement->fetch(PDO::FETCH_ASSOC))
{
array_push($eventArray, $row);
}
print_r($eventArray);
createXMLfile($eventArray);
Apache Log say PHP Notice: Undefined offset :0 from variables values in function write_records($eventArray)
I have been trying to print an output using json_encode, the code is below:
<?
$lid = $_GET['last_id'];
$sql = "SELECT * FROM tbl_posts WHERE id < $lid ORDER BY id DESC LIMIT 10";
$result = mysqli_query($con,$sql);
$json = include('datam.php');
echo json_encode($json);
?>
Here is the datam.php file:
<?
$count = 0;
while($row = mysqli_fetch_array($result)) {
$id = $row['id'];
$likes = $row['likes'];
$dislikes = $row['dislikes'];
$text = $row['text'];
$text = htmlspecialchars($text);
$title = substr($text, 0, 50);
$title = preg_replace('~[^\\pL\d]+~u', '-', $title);
$title = trim($title, '-');
$title = strtolower($title);
$title = preg_replace('~[^-\w]+~', '', $title);
if(empty($title)) {
$title = "no-title";
}
$cat = $row['cat'];
$cat = strtolower($cat);
$cat = str_replace(" ","-",$cat);
$ccat = str_replace("-"," ",$cat);
$ccat = ucwords($ccat);
$by = $row['uid'];
$dt = $row['date'];
$tm = $row['time'];
$time = strtotime("$dt $tm");
$nsfw = $row['nsfw'];
if ($nsfw == 1)
$isnsfw = '<span class="nsfw">NSFW</span>';
else
$isnsfw = "";
// Time Ago
// Get OP
$suser = "SELECT * FROM tbl_users WHERE id = $by";
$muser = mysqli_query($con,$suser);
$guser = mysqli_fetch_array($muser);
$byuser = $guser['user'];
$byuser = strtolower($byuser);
$byuser = str_replace(" ","-",$byuser);
$cbyuser = str_replace("-"," ",$byuser);
$cbyuser = ucwords($cbyuser);
// WhatsApp Link
$wm = preg_replace("/\n/", "%0A", $text);
if($count == 5 && $cnsfw == 0) {
include "adapp.php";
}
echo '<div class="box" id="'.$id.'"><div class="t">Posted by '.$cbyuser.' in '.$ccat.' '.ago($time).' ago</div><div class="m">'.$text.'</div><div class="m"><span class="col-3"><span class="likes">'.$likes.' Likes</span></span><span class="col-3"><span class="dislikes">'.$dislikes.' Dislikes</span></span><span class="col-3">'.$isnsfw.'</span></div><div class="b"><span class="col-5 l bbox"><img src="/img/like.png" /></span><span class="col-5 l bbox"><img src="/img/dislike.png" /></span><span class="col-5 l bbox"><img src="/img/comment.png" /></span><span class="col-5 l bbox"><a rel="nofollow" href="whatsapp://send?text=http://whatsappstatus.in/msg/'.$id.'%0A'.$wm.'"><img src="/img/whatsapp.png" /></a></span><span class="col-5 l bbox"><img src="/img/options.png" /></span></div><div class="clr"></div></div>';
$count++;
}
?>
Using this code, all the output appears as expected but, at the end of each output, there is unexpected output 1 which has no source of origin.
Every time this code executes, it gives an unexpected 1 at the end and I can't seem to find where it is coming from.
Here is the picture of sample output:
You can visit this URL to check it: http://funpd.com/messages1
When using
$json = include('datam.php');
In your datam.php you should return the value of the data you want assigning to $json rather than echoing it out. The echo in the datam.php is code which is displaying the data and the 1 is the value being assigned to $json (which is the value returned by your datam.php page).
You can test this by commenting out the echo in the sub page and you should just see the value 1 being displayed.
It may be the easiest solution looking at your code to just include this sub-page and not assign the value to $json, but you may have a reason for json encoding the return value.
Update:
To batch data up, build an array of data. At the top
$output = [];
After your echo - build an array of the results and add it to output array...
$output[] = [$id, $byuser, $cbyuser, ...];
(You will need to complete this with all of the fields you need)
And at the end
return $output;
This will pass all the data back to the calling page.
I got a small for each loop that I want to test. But it only shows me two empty list items. I also tested the SQL in phpmyadmin and it returns a correct table. I want to show the title from that table but like I said, I am getting two empty list items.
Does anybody know what I am doing wrong?
<?
// content
$content = "SELECT * FROM `snm_content` WHERE catid = 13";
$contentcon = $conn->query($content);
$contentcr = array();
while ($contentcr[] = $contentcon->fetch_array());
foreach($contentcr as $content)
{
$contentje .= '<li>'.$contentcr['title'].'</li>';
}
echo $contentje;
?>
Replace $contentcr by $content inside the foreach loop.
<?
// content
$content = "SELECT * FROM `snm_content` WHERE catid = 13";
$contentcon = $conn->query($content);
$contentcr = array();
while ($contentcr[] = $contentcon->fetch_array());
foreach($contentcr as $content)
{
$contentje .= '<li>'.$content['title'].'</li>'; // Here
}
echo $contentje;
?>
Try this:
while ($contentcr = $contentcon->fetch_array()) {
$contentje .= '<li>'.$contentcr['title'].'</li>';
}
echo $contentje;
Remove foreach use only while
while ($contentcr = $contentcon->fetch_array());{
$contentje .= '<li>'.$contentcr['title'].'</li>';
}
I have some problem with my syntax coding about emoticon.
it can replaced as well in first post, but in next post it can't replaced.
it not looping as well.
you can see the images in:
http://postimg.org/image/srph22j8d/
# POPULATED EMOTICON
$sqlEMO = "SELECT * FROM apprtcfg WHERE obj_typ = 'EMO' ORDER BY id ASC;";
$queryEMO = mysql_query($sqlEMO);
while ($rsltEmo=mysql_fetch_array($queryEMO)) {
$emo_code = $rsltEmo['obj_link'];
$emo_img = $rsltEmo['obj_source'];
}
echo $content = str_replace($emo_code,'<img src="image/'.$emo_img.'">', $row['content']);
You should replace your emotions with images, inside while and echo it after end of while.
$sqlEMO = "SELECT * FROM apprtcfg WHERE obj_typ = 'EMO' ORDER BY id ASC;";
$queryEMO = mysql_query($sqlEMO);
$content = $row['content'];
while ($rsltEmo=mysql_fetch_array($queryEMO)) {
$emo_code = $rsltEmo['obj_link'];
$emo_img = $rsltEmo['obj_source'];
$content = str_replace($emo_code,'<img src="image/'.$emo_img.'">', $content);
}
echo $content;
I have an rss feed, created by Yahoo Pipes and I need to get random post from it. How is it possible to realize this on php?
Read the feed using XML Parser and put it in an array. then, use array_rand to pick a random item from the array.
<?
function load_xml_feed($feed)
{
global $RanVal;
$i= 1;
$FeedXml = simplexml_load_file($feed);
foreach ($FeedXml->channel->item as $topic) {
$title[$i] = (string)$topic->title;
$link[$i] = (string)$topic->link;
$description[$i] = (string)$topic->description;
$i++;
}
$randtopic = rand(2, $i);
$link = trim($link[$randtopic]);
$title = trim($title[$randtopic]);
$description = trim($description[$randtopic]);
$RanVal = array($title,$link,$description);
return $RanVal;
}
$rss = "http://www.sabaharabi.com/rss/rss.xml";
load_xml_feed($rss);
$link = $RanVal[1];
$title = $RanVal[0];
$description = $RanVal[2];
echo "<h1>".$title."</h1><h2>".$link."</h2><p>".$description."</p>";