Jump to content

Trying to pass variables through a function for a featured content section


senshikarasu

Recommended Posts

How to explain this. I'm working with vbulletin 4 software right now and I'm trying to make a "scrolling features" section. The idea is to be able to place articles in a "featured content" category and have them dynamically appear at the top of the page in a formatted fashion. I successfully accomplished this by pulling the information directly out of the mysql table. Then it was decided to have a single Top Story section there as well. I also wanted to accomplish this by using a category name to dynamically place it at the top of the site, alongside the "scrolling features" section.

 

The problem I'm running into is doing both at once within the same script. I thought the best way to do it was to use functions since using two if statements resulted in failure. I tried all kinds of conditionals and looping statements, and now I'm stuck. I can't get it to display at all unless I'm only trying to do one section or the other, and trying to do it using functions only ends up spitting out one story in the "scrolling features" section. What can I do to get this on the right track?

 

I'm restricted in that the script has to end in the form of "$output = ". Any help at all would be greatly appreciated.

 

<?php

ob_start();

//The three functions below belong in a separate file
function truncate($text, $length, $suffix = '…', $isHTML = true){
	$i = 0;
	$simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
	$tags = array();
	if($isHTML){
		preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
		foreach($m as $o){
			if($o[0][1] - $i >= $length)
				break;
			$t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
			// test if the tag is unpaired, then we mustn't save them
			if($t[0] != '/' && (!isset($simpleTags[$t])))
				$tags[] = $t;
			elseif(end($tags) == substr($t, 1))
				array_pop($tags);
			$i += $o[1][1] - $o[0][1];
		}
	}

	// output without closing tags
	$output = substr($text, 0, $length = min(strlen($text),  $length + $i));
	// closing tags
	$output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');

	// Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
	$pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
	// Append closing tags to output
	$output.=$output2;

	// Get everything until last space
	$one = substr($output, 0, $pos);
	// Get the rest
	$two = substr($output, $pos, (strlen($output) - $pos));
	// Extract all tags from the last bit
	preg_match_all('/<(.*?)>/s', $two, $tags);
	// Add suffix if needed
	if (strlen($text) > $length) { $one .= $suffix; }
	// Re-attach tags
	$output = $one . implode($tags[0]);

	return $output;
}
//This is the first problem function. This does not spit out the info I need it to	
function contentstring($category, $previewtxt, $popular) {
 if($category == 41) {
	$content_bits .= "\t\t<li>\n\t\t<div class=\"preview_image\"><img src=\"".$popular[previewimage]."\" /></div>\n\t\t<h3><a href=\"/content/".$popular[nodeid]."\">".$popular[title]."</a></h3>\n\t\t<p>".$previewtxt."</p>\n\t\t</li>\n</ul>";
                                         }
return $content_bits;
}

//This is the second problem function. This does not spit out the info I need it to	
function newsstring($category, $previewtxt, $popular) {
 if($category == 42) {
	$news_bits .= "<div class="news"><div class=\"news_preview_img\"><img src=\"".$popular[previewimage]."\" /></div><h3><a href=\"/content/".$popular[nodeid]."\">".$popular[title]."</a></h3>\n\t\t<p>".$previewtxt."</p>\n\t\t</div>\n<ul id=\"ffxivblog\" class=\"jcarousel-skin-ffxivblog\">\n";
                                         }
return $news_bits;
}

  // Featured Content
  $popular_get = vB::$db->query_read("
    SELECT ".TABLE_PREFIX."cms_node.nodeid,
 ".TABLE_PREFIX."cms_node.contentid,
 ".TABLE_PREFIX."cms_article.pagetext,
 ".TABLE_PREFIX."cms_article.previewimage,
 ".TABLE_PREFIX."cms_nodecategory.categoryid,
 ".TABLE_PREFIX."cms_nodeinfo.title
FROM ".TABLE_PREFIX."cms_node
    LEFT JOIN ".TABLE_PREFIX."cms_article
ON ".TABLE_PREFIX."cms_node.contentid = ".TABLE_PREFIX."cms_article.contentid
LEFT JOIN ".TABLE_PREFIX."cms_nodecategory
ON ".TABLE_PREFIX."cms_node.nodeid = ".TABLE_PREFIX."cms_nodecategory.nodeid
LEFT JOIN ".TABLE_PREFIX."cms_nodeinfo
ON ".TABLE_PREFIX."cms_node.nodeid = ".TABLE_PREFIX."cms_nodeinfo.nodeid
    WHERE ".TABLE_PREFIX."cms_nodecategory.categoryid BETWEEN 41 AND 42");
  $content_bits = '';
  $news_bits = '';
  while($popular = vB::$db->fetch_array($popular_get)) {
    $item = $popular[pagetext];
    $item = preg_replace('[\[.{2,}/.{1,}\]]', '', $item);
    $item = preg_replace('[<object.{2,}/object>]', '', $item);
$previewtxt =  truncate($item, 80, $suffix = '…', $isHTML = true);
$category = $popular[categoryid];
$content_bits = contentstring($category, $previewtxt, $popular);
$news_bits = contentstring($category, $previewtxt, $popular);
  }
  $output =  $content_bits . $news_bits;
  ob_end_clean();

I was able to solve this with some help from someone else. Here's the code if anyone is interested.

 

Functions

function truncate($text, $length, $suffix = '…', $isHTML = true){
        $i = 0;
        $simpleTags=array('br'=>true,'hr'=>true,'input'=>true,'image'=>true,'link'=>true,'meta'=>true);
        $tags = array();
        if($isHTML){
            preg_match_all('/<[^>]+>([^<]*)/', $text, $m, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
            foreach($m as $o){
                if($o[0][1] - $i >= $length)
                    break;
                $t = substr(strtok($o[0][0], " \t\n\r\0\x0B>"), 1);
                // test if the tag is unpaired, then we mustn't save them
                if($t[0] != '/' && (!isset($simpleTags[$t])))
                    $tags[] = $t;
                elseif(end($tags) == substr($t, 1))
                    array_pop($tags);
                $i += $o[1][1] - $o[0][1];
            }
        }
        
        // output without closing tags
        $output = substr($text, 0, $length = min(strlen($text),  $length + $i));
        // closing tags
        $output2 = (count($tags = array_reverse($tags)) ? '</' . implode('></', $tags) . '>' : '');
        
        // Find last space or HTML tag (solving problem with last space in HTML tag eg. <span class="new">)
        $pos = (int)end(end(preg_split('/<.*>| /', $output, -1, PREG_SPLIT_OFFSET_CAPTURE)));
        // Append closing tags to output
        $output.=$output2;

        // Get everything until last space
        $one = substr($output, 0, $pos);
        // Get the rest
        $two = substr($output, $pos, (strlen($output) - $pos));
        // Extract all tags from the last bit
        preg_match_all('/<(.*?)>/s', $two, $tags);
        // Add suffix if needed
        if (strlen($text) > $length) { $one .= $suffix; }
        // Re-attach tags
        $output = $one . implode($tags[0]);
        
        return $output;
    }
    
function outputstring($category, $previewtxt, array $popular)
{
    $sResult = '';
    switch($category)
    {
        case 41:
            $sResult = "\t\t<li>\n\t\t<div class=\"preview_image\"><img src=\"".$popular['previewimage']."\" /></div>\n\t\t<h3><a href=\"/content/".$popular['nodeid']."\">".$popular['title']."</a></h3>\n\t\t<p>".$previewtxt."</p>\n\t\t</li>\n";
            break;
        case 42:
            $sResult = "<div class=\"news\"><div class=\"news_preview_img\"><img src=\"".$popular['previewimage']."\" /></div><h3><a href=\"/content/".$popular['nodeid']."\">".$popular['title']."</a></h3>\n\t\t<p>".$previewtxt."</p>\n\t\t</div>\n<ul id=\"ffxivblog\" class=\"jcarousel-skin-ffxivblog\">\n";
            break;
    }
    return $sResult;
}

 

Main Code

ob_start();
include('./scripts/custom-functions.php');
$popular_get = vB::$db->query_read("
    SELECT ".TABLE_PREFIX."cms_node.nodeid,
     ".TABLE_PREFIX."cms_node.contentid,
     ".TABLE_PREFIX."cms_article.pagetext,
     ".TABLE_PREFIX."cms_article.previewimage,
     ".TABLE_PREFIX."cms_nodecategory.categoryid,
     ".TABLE_PREFIX."cms_nodeinfo.title
    FROM ".TABLE_PREFIX."cms_node
    LEFT JOIN ".TABLE_PREFIX."cms_article
    ON ".TABLE_PREFIX."cms_node.contentid = ".TABLE_PREFIX."cms_article.contentid
    LEFT JOIN ".TABLE_PREFIX."cms_nodecategory
    ON ".TABLE_PREFIX."cms_node.nodeid = ".TABLE_PREFIX."cms_nodecategory.nodeid
    LEFT JOIN ".TABLE_PREFIX."cms_nodeinfo
    ON ".TABLE_PREFIX."cms_node.nodeid = ".TABLE_PREFIX."cms_nodeinfo.nodeid
    WHERE ".TABLE_PREFIX."cms_nodecategory.categoryid BETWEEN 41 AND 42");
  $content_bits = '';
  $news_bits = '';

  while($popular = vB::$db->fetch_array($popular_get)) {
$category = $popular['categoryid'];
$item = $popular['pagetext'];
$item = preg_replace('[\[.{2,}/.{1,}\]]', '', $item);
$item = preg_replace('[<object.{2,}/object>]', '', $item);
$previewtxt =  truncate($item, 80, $suffix = '…', $isHTML = true);

// These will need to be concatenated
    if ($category == 42)
    {
        $news_bits .= outputstring($category, $previewtxt, $popular);
    }
    else
    {
        $content_bits .= outputstring($category, $previewtxt, $popular);
    }
  }  
  $output =  $news_bits.$content_bits."</ul>";
  ob_end_clean();

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.