PDA

View Full Version : Image displaying from database


Mythotical
11-20-2009, 12:04 AM
I came up with my own way to pull images but the thing about is that it won't pull the image on vB4.0, here is the template and file.

Template:
<img src="download.php?catimg={vb:var cid}" height="50">

Template with pulling image data through different file other than download.php:
<img src="cat.php?img={vb:var cid}" height="50">

Keep in mind the var $cid is pulled in the download.php file while the image pull is a separate file but right now I want to attempt to combine the files into one.

File:
if ($vbulletin->options['bfc_download_active']){
$img = $_REQUEST['img'];

$result = $db->query_read("SELECT cat_icon,cat_type FROM download_cats WHERE catid=" . $img);
while ($row = $db->fetch_array($result) ) {
$sid = $row['cid'];
$type = $row['cat_type'];
$encodeddata = $row['cat_icon'];
}
$data = @mysql_RESULT($result,0,'cat_icon');
$type = @mysql_RESULT($result,0,'cat_type');

header( "Content-type: $type");
echo $data;
}

Code that is pulling the data for the $cid variable:
$sql = $vbulletin->db->query_read("SELECT * FROM `" . TABLE_PREFIX . "download_cats` WHERE cat_active = '1'");

$total = $vbulletin->db->num_rows($sql);
$i=0;
$perrow = $total-2;

while ($row = $db->fetch_array($sql))
{
$cid = $row['catid'];
$cat = $row['catitle'];
$description = $row['cat_description'];
$caticon = $row['cat_icon'];
$i++;

if ($total !== 0)
{

if ($total > $perrow){
$catc = '</td>';
$perrow = $perrow+2;
} else {
$perrow = $perrow-2;
$catc = '</td></tr><tr>';
}
$catb = vB_Template::create('bfc_download_cat_bit');
$catb->register('cat_bit', $cat_bit);
$cat_bit .= $catb->render();
}



Any help much appreciated.

Thanks
Steve M

cellarius
11-20-2009, 09:55 AM
Where do you register $cid for use in the template in the second chunk of code?

Mythotical
11-20-2009, 05:05 PM
while ($row = $db->fetch_array($sql))
{
$cid = $row['catid'];

Is that what you mean by registered? Or are you talking about cleaned?

cellarius
11-20-2009, 06:10 PM
You need to register variables to make them available in templates:
https://vborg.vbsupport.ru/showthread.php?t=228078

I thought you knew all that, since you're already doing it:
$catb = vB_Template::create('bfc_download_cat_bit');
$catb->register('cat_bit', $cat_bit);
$cat_bit .= $catb->render();

You need to do the same as in the second line for $catb for $cid.

Mythotical
11-20-2009, 06:21 PM
$cid is not a template or do all variables you use no matter if they are a template or not need to be registered?

I knew about templates but was unaware of having to do it for data variables such as $cid.

cellarius
11-20-2009, 06:29 PM
bfc_download_cat_bit is the template. You create it by calling the vB_Template:create method, and then you need to register every variable you want to use inside that template, no matter what's saved inside that variable. It can contain a rendered template, or it can contain just a simple boolean false or true. That does not matter, you need to register it to be able to use it. Then, in the next step, you render the template.

Mythotical
11-20-2009, 06:30 PM
Ok I can't figure out why this:
</td> </td></tr><tr>

Is displaying above my results when it should only use those if its starting a new row and ending another.

DavidsMods
11-20-2009, 10:32 PM
Just a quick note on security :)

$img = $_REQUEST['img'];
Should be cleaned as you are using it here:
$result = $db->query_read("SELECT cat_icon,cat_type FROM download_cats WHERE catid=" . $img);

Mythotical
11-20-2009, 10:36 PM
Thanks for pointing that out, I was actually starting to think I needed to clean it.