Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 03-12-2011, 02:58 PM While Loop Issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Suppose I did NOT want use a database row but instead from a path on the server to display the image, how would I work that into the code below? Note: I put the word ECHO (in CAPS lock) where the image part is:

PHP Code:
<?php

//Display Results 
$result mysql_query("SELECT * FROM adpeeps_ads LIMIT 1") or die(mysql_error());

echo 
"<table border=\"0\">";

while(
$row mysql_fetch_array($result)){

echo 
"<td>";
echo 
"Text Ad: <a href=\"$row[ad_url]\">$row[ad_title]</a>";
echo 
"<p />";
ECHO  
"Banner Ad: <img src=\"$row[images]\">";
echo 
"</td>";
}

?>
Thanks alot!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-12-2011, 03:14 PM Re: While Loop Issue...
Marik's Avatar
Skilled Talker

Posts: 99
Trades: 0
Are you saying the image is not going to be dynamic, instead you just want to always show the same image that's stored somewhere on your server?

If so then just change that line to:
PHP Code:
ECHO  "Banner Ad: <img src='http://www.mysite.com/path/to/imagefile.jpg' />"
__________________

Please login or register to view this content. Registration is FREE
Marik is offline
Reply With Quote
View Public Profile
 
Old 03-12-2011, 03:26 PM Re: While Loop Issue...
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
So you are trying to display all of the images in a particular directory?

PHP Code:
<?php
$dirname 
'path/to/directory/';
$dirurl 'http://site.com/url/to/directory';
$extensions = array('png''jpg''gif');

$images = array();
$dir = new DirectoryIterator($dirname);

foreach(
$dir as $item)
{
     if(
$item->isFile())
     {
          
$ext strtolower(substr(strrchr($item->getFilename(), '.'), 1));
          if(
in_array($ext$extensions))
          {
               
?>
Banner Ad: <img src="<?php echo $dirurl '/' $item->getFilename(); ?>" />
               <?php
          
}
     }
}
?>
Also, make sure to quote your array indexes. Your code will still work, but PHP will look for a constant before using the string value (and also generate a notice).
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 03-12-2011, 03:45 PM Re: While Loop Issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Thanks for *both* of your replies!

Ok, Matt, your code is more like what I was looking for, but let me explain a bit more. The script I am using is an ad management script and it store both text and banner information in the db. Ex: Link impressions, Link clicks, urls etc.. I was told from the scripts owner that if I store the images in the database ( as blob ), the site will become very slow. So instead, I am not storing the images in the db, so the script uses a default path to store the images and that's what I will be using, but I have added a pagination script so that the banners can have a limit per page. That's what the above code came from .. the pagination script.

Edit:: I am using the url from the db, I don't know if that will slow it (website) down, but I will try that...My problem now is my pagination script is showing 2 results when there's only one in the db. I am working out that kink atm. If you have a solution to the above, then by all means post it.

Thanks again!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 03-12-2011 at 04:39 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 03-12-2011, 04:49 PM Re: While Loop Issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Ok, I have two ads, in my db, one Text and one Banner ad, I am using the following code to display ONLY the one Banner ad:

PHP Code:
<?php


$result 
mysql_query("SELECT * FROM adpeeps_ads WHERE ad_type = 'image/png' LIMIT 1") or die(mysql_error());

echo 
"<table border=\"0\">";

while(
$row mysql_fetch_array($result)){

echo 
"<td>";
echo 
"Banner Ad: <img src=\"$row[remote_banner]\">";
echo 
"</td>";
}

?>
How come my pagination code is displaying two pages with the same banner from the db on the two pages? Banner on page one, and the same banner on page 2. I did not echo out the text ad so I don't expect that one to display. But why two banners when there's only one in the db?


Here's the Pagination code:

PHP Code:
<?php
            
/*
        Place code to connect to your DB here.
             */

            
include('config.php');    // include your code to connect to DB.

            
$tbl_name="adpeeps_ads";        //your table name

            // How many adjacent pages should be shown on each side?
            
$adjacents 3;

              
/* 
            First get total number of rows in data table. 
            If you have a WHERE clause in your query, make sure you mirror it here.
            */

            
$query "SELECT COUNT(*) as num FROM $tbl_name";
                
$total_pages mysql_fetch_array(mysql_query($query));
                
$total_pages $total_pages[num];

            
/* Setup vars for query. */
            
$targetpage "Animals, Pets & Accessories.php";     //your file name  (the name of this file)

            
$limit 1;              //how many items to show per page

            
$page $_GET['page'];
            if(
$page
            
$start = ($page 1) * $limit//first item to display on this page

            
else
        
$start 0;           //if no page var is given, set start to 0
            /* Get data. */

            
$sql "SELECT remote_banner FROM $tbl_name LIMIT $start$limit";
                
$result mysql_query($sql);

            
/* Setup page vars for display. */
            
if ($page == 0$page 1;               //if no page var is given, default to 1.
            
$prev $page 1;                  //previous page is page - 1
            
$next $page 1;                 //next page is page + 1
            
$lastpage ceil($total_pages/$limit);    //lastpage is = total pages / items per page, rounded up.
            
$lpm1 $lastpage 1;            //last page minus 1

                /*
        Now we apply our rules and draw the pagination object. 
        We're actually saving the code to a variable in case we want to draw it more than once.
            */

            
$pagination "";
            if(
$lastpage 1)
             {
        
$pagination .= "<div class=\"pagination\">";

        
//previous button

        
if ($page 1
        
$pagination.= "<a href=\"$targetpage?page=$prev\"> previous </a>";
        else
        
$pagination.= "<span class=\"disabled\"> previous </span>";

        
//pages

        
if ($lastpage + ($adjacents 2))    //not enough pages to bother breaking it up
        
{    
        for (
$counter 1$counter <= $lastpage$counter++)
        {
        if (
$counter == $page)
        
$pagination.= "<span class=\"current\">$counter</span>";
        else
        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";                
        }
        }
        elseif(
$lastpage + ($adjacents 2))  //enough pages to hide some
        
{

        
//close to beginning; only hide later pages

        
if($page + ($adjacents 2))        
        {
        for (
$counter 1$counter + ($adjacents 2); $counter++)
        {
        if (
$counter == $page)
        
$pagination.= "<span class=\"current\">$counter</span>";
        else
        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
                }
        
$pagination.= "...";
        
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
        
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
                }

        
//in middle; hide some front and some back

        
elseif($lastpage - ($adjacents 2) > $page && $page > ($adjacents 2))
        {
        
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
        
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
        
$pagination.= "...";
        for (
$counter $page $adjacents$counter <= $page $adjacents$counter++)
        {
        if (
$counter == $page)
        
$pagination.= "<span class=\"current\">$counter</span>";
        else
        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</ a>";                
        }
        
$pagination.= "...";
        
$pagination.= "<a href=\"$targetpage?page=$lpm1\">$lpm1</a>";
        
$pagination.= "<a href=\"$targetpage?page=$lastpage\">$lastpage</a>";        
        }

        
//close to end; only hide early pages

                
else
        {
        
$pagination.= "<a href=\"$targetpage?page=1\">1</a>";
        
$pagination.= "<a href=\"$targetpage?page=2\">2</a>";
        
$pagination.= "...";
        for (
$counter $lastpage - (+ ($adjacents 2)); $counter <= $lastpage$counter++)
        {
        if (
$counter == $page)
        
$pagination.= "<span class=\"current\">$counter</span>";
        else
        
$pagination.= "<a href=\"$targetpage?page=$counter\">$counter</a>";
        }
        }
        }
        
        
//next button

        
if ($page $counter 1)
        
$pagination.= "<a href=\"$targetpage?page=$next\"> next </a>";
        else
        
$pagination.= "<span class=\"disabled\"> next </span>";
        
$pagination.= "</div>\n";        
            }
?>
Thank you for your help!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE

Last edited by Brian07002; 03-12-2011 at 04:51 PM..
Brian07002 is offline
Reply With Quote
View Public Profile
 
Old 03-12-2011, 05:06 PM Re: While Loop Issue...
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
Ok, I got it! I forgot to add the limit to mysql $query in the pagination code. Now it is working as expected!

Thanks all!
-Brian
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to While Loop Issue...
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.28889 seconds with 12 queries