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;
Related
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 am trying to get clean URLs of localities belong to associated district where a clean url function is applied to $_GET varialble. I've two pages, 1st one is district.php who has links to associated localities using this function to generate clean URLs. Below is the code of district.php page :-
<?php
$remove[] = " ";
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
$string = preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
return $string;
}
include($_SERVER['DOCUMENT_ROOT'].'/database-path.php');
$district = str_replace('-',' ',$_GET['district']);
$state = str_replace('-',' ',$_GET['state']);
$sql="SELECT * FROM table_name WHERE district='$district' AND state='$state'";
$result = mysqli_query($con,$sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$id = $row['id'];
$district = $row['district'];
$locality = $row['locality'];
$state = $row['state'];
$iso = $row['iso'];
}
?>
On the same district page, below code is used to generate clean URLs of associated localities :-
" . $row["locality"]. "
The above code is generating URLs to all localities pages in the manner of "locality-page-url-with-decoded-id-number" on district page.
Below is the code of locality.php where I'm trying to apply same clean function to $_GET variable in order to get clean URLs
<?php
function clean($string) {
$string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
$string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
$string = preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
return $string;
}
include($_SERVER['DOCUMENT_ROOT'].'/database-path.php');
$locality = clean($_GET['locality']);
$localID = base64_decode($_GET['id']) ;
$sql="SELECT * FROM table_name WHERE locality='$locality' AND id='$localID'";
$result = mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($result);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$locality = $row['locality'];
$content = $row['content'];
$district = $row['district'];
$state = $row['state'];
$iso = $row['fld_iso'];
}
}
?>
htacces code :-
RewriteRule ^([^/]*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]
Now the problem I'm facing is that the $locality = clean($_GET['locality']); is not working here and is returning to error 404. It is not getting cleaned values of localities. When I try the same manually after removing clean function from $_GET variable using all characters available in the field, it works fine. Is there any way that I can use ID as the main variable in order to get row values having locality's clean values in the URL of the page?
I finally fixed it by working around the code.
Here is the new htaccess code making first parameter optional and picking ID as the main parameter to work properly:-
RewriteRule ^(.*)-([^/]*)$ locality.php?locality=$1&id=$2 [L]
Changes done on locality.php
Minor changes are done to use ID to fetch the rows in the following manner :-
<?php
include($_SERVER['DOCUMENT_ROOT'].'/database-path.php');
$localID = base64_decode($_GET['id']) ;
$sql="SELECT * FROM table_name WHERE id='$localID'";
$result = mysqli_query($con,$sql);
$rowcount=mysqli_num_rows($result);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$locality = $row['locality'];
$content = $row['content'];
$district = $row['district'];
$state = $row['state'];
$iso = $row['fld_iso'];
}
}
?>
Thank you everyone for your help.
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>';
}
In my PHP forum i want people to insert pictures just by inserting the number/ID of the picture (which they can see in an online photoalbum)
I am looking for a function that can read a string from their posts for example
"bla bla and look at this amazing picture [IMG]234[/IMG] isn't it awesome ..."
then finds the picture in a database with the ID 234 and replaces [IMG]234[/IMG] with
<img src = "path/to/image.jpg" />
preg_replace wouldn't work :( does anyone have an Idea?
thanks for Your help in advance
I would do it in this way:
$images = array();
$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) use(&$images) {
$images[] = $matches[1];
return '__image_' . $matches[1];
}, $post);
if (count($images)) {
// Select images
$imageIds = implode(',', $images);
// DB query
$res = mysql_query("SELECT id, path FROM post_images WHERE id IN ({$imageIds})") or die(mysql_error());
// Replace
while (($row = mysql_fetch_assoc($res))) {
$post = str_replace('__image_'.$row['id'], '<img src=' . $row['path'] . ' />', $post);
}
}
The advantage is that you make only one query to database. This is always important to minimize them to increase performance. if you don't care or you are sure that the number of images in not going to be too high, you can simple use this code:
$post = preg_replace_callback('|\[img\](\d+)\[/img\]|i', function($matches) {
$res = mysql_query("SELECT path FROM post_images WHERE id = {$matches[1]}") or die(mysql_error());
$path = mysql_result($res, 0);
return "<img src='$path' />";
}, $post);
here is what i did:
$find = preg_match_all("!\[img\][0-9]+\[\/img\]!", $post, $matches);
foreach ($matches as $match) {
foreach ($match as $ma) {
$res = str_replace("[/img]","", str_replace("[img]", "",$ma));
$query = "
SELECT
path
FROM
table
WHERE id = '".$res."'
";
$result = mysql_query($query, $conn) or die(mysql_error());
while($line = mysql_fetch_array($result)) {
$path = $line["path"];
$path = "<img src = '".$path. "'></img>";
$post = str_replace ("[img]" . $res . "[/img]", $path, $post);
}
}
}
note: i don't know why preg_match_all creates a 2 depths array
Your code needs to make a db call to get the path from the ID.
Use a tag parser or basic regex to match \[img][0-9]+\[/img]. (preg_match)
Then query your database for the path with the match result as the ID.
Finally, use str_replace to replace the original "[img]$match[/img]" with
Example (pseudo, look these functions up first)
$matches = preg_match_all('\[img][0-9]+\[/img]', $input);
$output = $input;
foreach ($matches as $match) {
$cur = mysql_query("SELECT path FROM table WHERE ID = $match");
$row = mysql_fetch_array($cur);
$output = str_replace('[img]'.$match.'[/img]',$row['path'],$output);
}
Note: this will only work with tags in lowercasse: img and not with IMG, iMG, ImG and so on.
Why doesn't preg_replace work? Not knowing the the details, I would guess that you are not escaping the square brackets. What does your regular expression look like? Try something like
/\[IMG\](\d+)\[/IMG]/
The image id will then be in the group 1, i.e. $1.
Example:
preg_match_all('/\[IMG\](\d+)\[/IMG]/', $post, $matches);
foreach ($matches as $match) {
echo "Image number: " . $match[1] . "\n";
}
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.