PHP - Rss To Php - Limit Description & No Posts
Hi,
I've been passed the below code which converts rss to php and then displays HTML. It works well. However, I've 2 issues with it: 1) I'd like to set it so only say 5 posts are shown 2) How can I truncate the description, so it only shows say 100 chars or 20 words? Any help is much appreciated. Here's the code: <?php // DO NOT EDIT BELOW THIS LINE date_default_timezone_set("GMT"); $itemArray = array(); if($text = preg_replace("/[\t\r\n]/", " ", htmlentities(@file_get_contents($rss_feed_url), ENT_QUOTES))) { while(strpos($text, "â") == true) { $text = substr($text, 0, strpos($text, "â")) ."'". substr($text, strpos($text, "â")+9, strlen($text)); } $items = explode("<item>", html_entity_decode($text)); foreach($items as $item) { $title = ""; $link = ""; $desc = ""; $pubdate = ""; $image = ""; $item = html_entity_decode($item, ENT_QUOTES); $item = preg_replace('/&(?!amp;|quot;|nbsp;|gt;|lt;|laquo;|raquo;|copy;|reg;|bul;|rsquo;|mdash;|#)/', '&', $item); $item = str_replace("<![CDATA[","", $item); $item = str_replace("]]>","", $item); if(stripos($item, "</item>") == true) { if(preg_match('|<title>(.*)</title>|', $item, $match)) { $title = $match[1]; } if(preg_match('|<pubDate>(.*)</pubDate>|', $item, $match)) { $pubdate = strtotime($match[1]); } if(preg_match('|url="(.*)|', $item, $match)) { $search = $match[1]; $imageArray = explode('"', $search); $image = $imageArray[0]; } if(preg_match('|<description>(.*)</description>|', $item, $match)) { $desc = $match[1]; $desc = strip_tags($desc); } if(preg_match('|<link>(.*)</link>|', $item, $match)) { $link = $match[1]; $link = end(explode("*", $link)); } if($title && $link && $desc) { array_push($itemArray, "$pubdate|$title|$link|$desc|$image|"); } } } } else { print "<p>Whoops! We could not connect to: <a href=\"$rss_feed_url\">$rss_feed_url</a></p>"; } if(sort($itemArray)) { foreach(array_reverse($itemArray) as $iA) { list($pubdate,$title,$link,$desc,$image) = explode("|", $iA); $timenow = time(); $difference = $timenow-$pubdate; $days = 0; $hours = 0; $minutes = 0; print "<p>"; if($hotlink_images && $image) { if($open_links_in_new_window) { print "<a class=\"thumb\" href=\"$link\" onclick=\"openInNewWindow('$link'); return false\">" . "<img src=\"$image\" alt=\"$title\"></a>"; } else { print "<a class=\"thumb\" href=\"$link\"><img src=\"$image\" alt=\"$title\"></a>"; } } if($open_links_in_new_window) { print "<a href=\"$link\" onclick=\"openInNewWindow('$link'); return false\">$title</a><br>"; } else { print "<a href=\"$link\">$title</a><br>"; } if($pubdate) { print "<span class=\"sub\">Posted "; if(sprintf("%01.0d", $difference/(60*60*24))) { $days = sprintf("%01.0d", $difference/(60*60*24)); $difference -= $days*(60*60*24); if($days > 1) { print "$days days "; } else { print "$days day "; } } if(!(sprintf("%01.0d", $difference/(60*60*24))) && sprintf("%01.0d", $difference/(60*60))) { $hours = sprintf("%01.0d", $difference/(60*60)); $difference -= $hours*(60*60); if($hours > 1) { print "$hours hours "; } else { print "$hours hour "; } } if(!(sprintf("%01.0d", $difference/(60*60*24))) && sprintf("%01.0d", $difference/60)) { $minutes = sprintf("%01.0d", $difference/60); if($minutes > 1) { print "$minutes minutes "; } else { print "$minutes minute "; } } print "ago</span><br>"; } print "$desc</p>\n<div class=\"hr\"> </div>\n"; } } ?> Thanks again Similar TutorialsUnless buffer overflows or breaking out of code to perform a new command are problems that have been solved.... I am trying to figure out the proper PHP method for setting a boundary on a variable within a script. I have this variable $name which is fed a value from $_POST['name'] from a form field. Now this form field is limited in the HTML to accept only 20 characters, but someone could easily edit the form or outgoing post data. So I want to know how to limit the variable size in the script. In other languages it could be something like this: var name(20). So how do I do that in PHP? Hi I have the following code, this code is suppose to get the data from the galleries table and the pages table. I think my problem is the sql statement but I'm not sure. Code: [Select] public function galleries(){ foreach($this->_params['list'] as $gallery){ $id = $gallery['id']; $name = $gallery['name']; $select = "SELECT * FROM pages, galleries WHERE pages.name = {$gallery['name']}"; $this->_params['item'] = $this->_model->get(array("pages.name = 'galleries.name'")); } the code Code: [Select] $this->_params['item'] = $this->_model->get(array("pages.name = 'galleries.name'")); works just fine with the home page and all the other pages. I have 2 galleries called gallery 1 and gallery 2, now my problem is that I need the sql to go through the galleries table look for the name and then look through the pages table and get the meta data from the same name. There is no foreign id because I only need it for the galleries and nothing else. Thank you so i set names for every pages, like $pageName = 'home'; //index page Code: [Select] if($pageName == 'categories'){ $find = "Birds & Bees"; $result = strpos($title,$find); echo '1'; }else if($pageName == 'categories'){ $find1 = "Money & Honey"; $result = strpos($title,$find1); echo '2'; } now the issue is with categories.php page, which is just one page but displays different title and according to matching title i wish to change the description but its not happening, everywhere only 1 prints, why? I set up the following code to successfully individual items based on the id number. ?id=1 etc. However, I thought it would be simple to change to show another row so I changed all the terms to 'description'. However, if I enter ?description=abcde it shows nothing. But if I type in ?description=description is bizarrely shows everything. The only thing I can only put it down to is numbers. Does $_GET react differently react differently to numbers or does it require commas surrounding the string? <?php if( isset($_GET['description'])) $_GET['description']; $query = "SELECT * FROM productfeed WHERE description = $description LIMIT 0, 10"; $fetchdata = mysql_query($query) or die("query: $query<br>This has an error: " . mysql_error() . '<br>'); while($row = mysql_fetch_array($fetchdata)) { $id = $row['id']; $image = $row['awImage']; $link = $row['link']; $description = $row['description']; $fulldescription = $row['fulldescription']; $price = $row['price']; echo "<div class='productdisplayshell'> <div class='productdisplayoutline'> <div class='productborder'><center> <a href='$link' target='_blank'><img src='$image'/></a> </center> </div></div> <div class='productdescriptionoutline'> <div class='productdescriptionbox'> <a href='$link' target='_blank' >$description</a> </div> <div class='productfulldescriptionbox'>$fulldescription</div> </div> <div class='productpriceoutline'> <div class='productpricebox'> <center>&#163; $price</center> </div> <div class='productbuybutton'> <center><a href='$link' target='_blank' ><img src=/images/buybutton.png /></a></center> </div> </div> </div>"; } ?> Hi I am trying to pull title and meta description from an URL.. To pull the URL I am using the code from http://www.dreamincode.net/code/snippet4625.htm... function getTitle($Url){ $str = file_get_contents($Url); if(strlen($str)>0){ preg_match("/\<title\>(.*)\<\/title\>/",$str,$title); return $title[1]; } } //Example: echo getTitle("http://www.cnn.com"); And to pull the meta description I am using the code from http://php.net/manual/en/function.get-meta-tags.php. // Assuming the above tags are at www.example.com $tags = get_meta_tags('http://www.cnn.com/') or die("Could not fetch meta tags"); // Notice how the keys are all lowercase now, and // how . was replaced by _ in the key. echo $tags['author']; // name echo $tags['keywords']; // php documentation echo $tags['description']; // a php manual echo $tags['geo_position']; // 49.33;-86.59 So my complete code looks like this: <html> <head> </head> <body> <?php function getTitle($Url){ $str = file_get_contents($Url); if(strlen($str)>0){ preg_match("/\<title\>(.*)\<\/title\>/",$str,$title); return $title[1]; } } //Example: echo getTitle("http://www.cnn.com"); // Assuming the above tags are at www.example.com $tags = get_meta_tags('http://www.cnn.com/') or die("Could not fetch meta tags"); // Notice how the keys are all lowercase now, and // how . was replaced by _ in the key. echo $tags['author']; // name echo $tags['keywords']; // php documentation echo $tags['description']; // a php manual echo $tags['geo_position']; // 49.33;-86.59 ?> Test! </body> </html> But it wont work... All I get is "Could not fetch meta tags".. I've tried different URLs and such but still wont work .. Anyone have an idea? Hello everybody, I have a question I've been striving with for a while and I was wondering if someone more experienced than me could have an answer or point me in the right direction. I'm building a website with an integrated forum what is the best system to detect and return which post is 'new', which one is 'hot' and which one hasn't changed from the user's last visit? In my user's table I store the last time the user logged in and the last time the user browsed a page... but it seems to be not enough to accurately return the information I need... Any suggestion is appreciated. Thanks. Hi. I'm making a website where users can upload files along with title, description and image. I can get them making the page (str replace) but I only know how to list the files with just the file title in a basic links directory. Any ideas how I could include title, description and a picture (as well as the link to the page). Sort of like with Youtube search results or engine001.com/games.htm? Any help would be greatly appreciated. Thanks I have a website here [redacted] and it is an affiliate website where I just find products and promote them. All I have to do is give product a title, affiliate url, the description, an image and a price. I am trying to make the product description area hold more text than it does now, because at the moment in the admin side of my website it says "product description 275 max characters" That is really a short sentence. Can anyone please help me I have no php skills but really wish I did, if you need more info I will be happy to share anything I need to. Edited October 27, 2019 by requinixremoved link im trying to run a query which will get posts from the database that have tags similar to the current ones. Unfortunatly the echoed result isnt displaying as it should. $query = $link->query("SELECT t.*, p.* FROM ".TBL_PREFIX."topics t JOIN ".TBL_PREFIX."posts p ON (p.p_tid = t.t_tid) WHERE t.t_name = '".decode_url($_GET['t_name'])."' LIMIT 1") or die(print_link_error()); $row = $query->fetch(PDO::FETCH_ASSOC); $tag_query = $link->query("SELECT p_name FROM ".TBL_PREFIX."posts WHERE MATCH (p_tags) AGAINST ('".$row['p_tags']."' IN BOOLEAN MODE) ")or die(print_link_error()); $result = $tag_query->fetch(); foreach($result as $key => $value) { echo 'Name = '.$result[$key]['p_name'].'<br />'; } i have done a dump of result and it shows: Code: [Select] $ => Array (2) ( ['p_name'] = String(12) "File Updates" ['0'] = String(12) "File Updates" ) but there should only be one item in this array(p_name). I dont know where the second is coming from. The query has pulled the right results, it just isnt displaying it right. I have made a website that deals with the sales of different products. It handles the total sales of a specific product. One of the aspects of the website is that it can change the current sales data of the products. At the moment I can change the sales figures fine however I have to click the submit button once and then physically refresh the page for it to change the data in the table. Is there a way that I can automatically refresh the page once the submit button is pressed? I have tried using headers for example: Code: [Select] if (!empty($_POST['NSales'])) { header('location: currentpage.php'); } however this just constantly refreshes the page. Any help would be greatly appreciated Hi there, I have a piece of code that takes all posts with the category of 'model' and puts it on the models page. It then displays the post as a gallery using fancy box. The problem is I want all posts with the category of 'faces' to go on the faces page, but at the moment they are appearing on both pages. Here is the code I have. Any help will guarantee a donation as I am at my wits end trying to figure this out. Code: [Select] <?php global $post; $args = array( 'numberposts' => 8, 'offset' => 0, 'category' => 'models', 'orderby' => 'post_date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish' ); $posts_array = get_posts( $args ); $x = 1; // track model foreach( $posts_array as $post ) { setup_postdata($post); ?> <div id="model-thumb"> <?php $arg2 = array( 'post_type' => 'attachment', 'numberposts' => -1, 'order' => 'ASC', 'post_status' => null, 'post_parent' => $post->ID ); $attachments = get_posts($arg2); if ($attachments) { $i = 1; // track images foreach ( $attachments as $attachment ) { $image_attributes = wp_get_attachment_image_src( $attachment->ID, 'full' ); if ($i == 1) { // Get thumbnail info to display with second image $img1src = $image_attributes[0]; $img1w = $image_attributes[1]; $img1h = $image_attributes[2]; } elseif ($i == 2) { // Display thumbnail with link to second image ?> <a class="allModels" rel="gallery<?php echo $x; ?>" href="<?php echo $image_attributes[0]; ?>" ?><img src="<?php echo $img1src; ?>" width="<?php echo $img1w; ?>" height="<?php echo $img1h; ?>"/></a><br /><p><?php the_content();?></p></div> <?php } else { // Other images ?> <div style="display:none;"><a class="allModels" rel="gallery<?php echo $x; ?>" href="<?php echo $image_attributes[0]; ?>"><img src="<?php echo $image_attributes[0]; ?>" width="<?php echo $image_attributes[1]; ?>" height="<?php echo $image_attributes[2]; ?>"/></a></div> <?php } $i++; // Increase image count } } $x++; // Increase model count } ?> Hey! So, I don't know if this is the current section, couldn't really decide on what section/board to post it, so ..
I've got a code that I got from a friend of mine (it was a gaming website (a game server), but he shut it down, I asked for some scripts, he sent me the whole site ..). I got all other scripts correctly, but I do not know how to get this Last Forum Posts to work (IP.Board). I've got the SEO set up as it should and everything, doesn't work.
When I use this
<?php $_handlerForum = new mysql("Forum","localhost","*********_fqipb2","*********_fqipb2","password"); $topics = $_handlerForum->fetch("SELECT `tid`,`title`,`last_post`,`title_seo`,`last_poster_name` FROM `topics` ORDER BY `last_post` DESC LIMIT 0,3;",array()); $content = ""; foreach($topics as $topic) { $content .= "<a href='http://website.com/forum/index.php?/topic/".$topic['tid']."-".$topic['title_seo']."' target='_blank'>".$topic['title']."</a><br /> <small class='dim'>".$topic['last_poster_name']." - ".date("H:i F d, Y",$topic['last_post'])."</small><hr style='margin: 5px 0px;' />"; } return $content; return "Under Construction"; ?>I get this error Fatal error: Class 'mysql' not found in /home/***/***/***/public_html/extra/forum.php on line 2Thought that the problem was "new mysql" (on line 2), so I put in mysql_connect, still get an error. Can anyone help? Hi, I have a little problem that I can't seem to solve myself. My coding skills are very limited, just wanted to mention that . I have a list of rows, bascially banners that members submit. They have to be manually approved by me before they are put on air. So to quicken up this process I made this little code in my admin panel. $bq = mysql_query("SELECT id, usrid, bname, burl, btarget FROM ban_rotator WHERE status = 'Waiting'"); $pb = mysql_num_rows($bq); if ($_GET['ap']) { $bid = $_GET['ap']; mysql_query("UPDATE ban_rotator SET status = 'Active' WHERE id = '$bid'"); It works perfectly except for one thing. It will always show the banner I just approved in addition to the remaining ones that are waiting. Any tips would be appreciated. I have to add that this code is a part of a fairly large admin panel php file. Just realized i need a bit more explanation. If I hit refresh in the browser the one I just approved will be gone. As I would like it to be after hitting Approve on the banner in question. If I just approve the second banner (after approving the first), the first will be gone from the list of waiting and the second will still show until I either hit approve on the third or hit refresh in the brower. Alka G'day all, i've got this script that I found on the net some time back but not entirely sure where, however it works well except I want to add a feature so it shows the image details from a txt file. Here is the gallery script: <?php function getDirectory( $path = '.', $level = 0 ){ $ignore = array( 'cgi-bin', '.', '..' ); $dh = @opendir( $path ); while( false !== ( $file = readdir( $dh ) ) ){ if( !in_array( $file, $ignore ) ){ if( is_dir( "$path/$file" ) ){ $dir = $path . $file; echo "<h2>$file</h2><p>"; $dh1 = @opendir( $dir ); while( false !== ( $image = readdir( $dh1 ) ) ){ if( !in_array( $image, $ignore ) ){ if( !is_dir( "$dir/$image" ) ){ if (preg_match("/.jpg/", $image)) { echo "<a href=\"$dir/$image\" rel=\"lightbox\"><img src=\"gallery-thumbnail.php?file=$file/$image\" border=2 style='border-color: #981C1E'></a>\n"; } } } } echo "</p>"; $level++; } } } closedir( $dh ); } getDirectory('gallery/'); ?> Now, what i'm wanting is to read a txt file either in the root or 'gallery' directory, where it has the file name, title, description, photographer. (something like filename.jpg|title of image|description about image|J King). so when the image is displayed, a script reads the text file and will show the image info beside it. Now i've got this bit of snippet from an old script I used once, but can't seem to get it to work anywhere, so any help is greatly appreciated. $text = "gallery.txt"; $fh=fopen($text, "r"); while(!feof($fh)) { $temp = explode("|", $line); $title[$temp[0]] = $temp[1]; $description[$temp[0]] = $temp[2]; $photographer[$temp[0]] = $temp[3]; $line=fgets($fh); unset($temp); } hi all,
Firstly I am new to the php language,
hopefully this is not a silly question or a no brainer.
I have looked over my code.. and for some reason when I insert data from a cms into a mySql database thers two fields that swop around..
HERES THE CODE IM WORKING WITH :
<?php //if form has been submitted process it if(isset($_POST['submit'])){ $_POST = array_map( 'stripslashes', $_POST ); //collect form data extract($_POST); //very basic validation if($title ==''){ $error[] = 'Please enter the title.'; } if(!isset($error)){ try { //insert into database $stmt = $handler->prepare('INSERT INTO event_calendar (title,event_date,description) VALUES (:title, :description, :event_date)') ; $stmt->execute(array( ':title' => $title, ':description' => $description, ':event_date' => date('Y-m-d') )); //redirect to index page header('Location: index.php?action=added'); exit; } catch(PDOException $e) { echo $e->getMessage(); } } } //check for any errors if(isset($error)){ foreach($error as $error){ echo '<p class="error">'.$error.'</p>'; } } ?> <form action='' method='post'> <p><label>Title</label> <input type='text' name='title' value='<?php if(isset($error)){ echo $_POST['title'];}?>'></p> <p><label>Description</label><br /> <textarea name='description' cols='50' rows='5'><?php if(isset($error)){ echo $_POST['description'];}?></textarea></p> <p><label>Date of Event : (y-m-d) :</label><input name="event_date" type="date" value='<?php if(isset($error)){ echo $_POST['event_date'];}?>'></p> <p><input type='submit' name='submit' value='Submit'></p> </form>Could anyone please just look through it.. My database structure is simple.. id, title, description, event_date Thanks in advance How can I prevent the less than and greater than signs in the username field, and message on the post? As well as slashes. / \. Code: [Select] <form method="post" action="" id="reply"> <script type="text/javascript"> function hi(id){ var val = id.options[id.selectedIndex].value; var text = id.options[id.selectedIndex].text; if (val.length != 0){document.getElementById('user').value = text;hide('passrow');} else { document.getElementById('user').value = ''; document.getElementById('passrow').style.cssText = ''; } } </script> <div id="userpass" style="background: #000;"> <table style="border: 2px solid #252564; background: #1F1F5D;" width="100%" cellpadding="0" class="quick_userpass"> <tr> <td background="/images/bg3.jpg" height="26px"><font face=arial size=2 color="white"> <b>Your Name or Nickname</b></td> </tr> <tr> <td><input size=50 name="user" id="user" value="" style="margin-left: 10px;"></td> </tr> <tr><td background="/images/bg3.jpg" height=26px></td></tr> </table> <div id="passrow" style=""> <p> <table width=100% cellpadding=0 class="quick_userpass" style="border: 2px solid #252564; background: #1F1F5D;"> <tr><td background="/images/bg3.jpg" height=26px style=""><font face=arial size=2 color="white"> <b>Password (optional)</b></td></tr> <tr><td style=""><input size=50 name="pass" id="pass" type="password" style="margin-left: 10px;" value=""></td></tr> <tr><td background="/images/bg3.jpg" height=26px></td></tr> </table> </p> </div> <p> <table width=100% cellspacing=0 cellpadding=4 class="quick_userpass" style="border: 2px solid #252564; background: #1F1F5D;"> <tr bgcolor="#121236"><td><font face=arial size=2 color="white"><b>Enter your message here</b></font></td></tr> <tr><td><center><textarea id="quickreply" name="message" rows=10 cols=50 wrap="VIRTUAL" ></textarea></center></td></tr> </table> <div id="preview" style="display: none;"></div> </p> <br /> <center> <font face=arial size=2>When you're happy with your message, click:</font> <div id="javano"><input type="submit" value=" Post Message "></div> </center> <input type="hidden" name="tid" value="<?php echo $_GET["tid"];?>" /> </div> </form> That is the code I'm using for User/Pass/message fields. example : Code: [Select] <?php code; ?> how to change CSS (i guess this is the method) using php or javascript to organize text/code like <code></code> here ! i did a search and didnt found anything useful! Hi all. This seems like a pretty basic function but I can't work it out. I'm developing a WordPress site and want to list posts (products) by category. I'm using Advanced Custom Fields and have set up the taxonomy of 'category'. I just want to produce a heading for that category and then list all of the posts with that category assigned. Please see below functions.php excerpt and section-archive.php excerpt. Can anyone please help??? Massive good karma if you can. ----------------------- functions.php -------------------------------------- //Assign Category to Products
function taxonomy ()
$args = array (
}; ?> ------------------------- section-archive.php ------------------------------------------------ <?php if(have_posts()):while(have_posts()): the_post();?>
****** if product category= this ***** Category heading ****** Posts ******* Else ****** if product category= this2 ***** Category heading ****** Posts **** etc
<div class="prod-listing">
i have a problem..my posts are showing up like this: July 6: post message July 6: post message July 6: post message im trying to get it like this: July 6: post message post message post message how do i group them like this in all in one post? |