Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions

Reply
 
Thread Tools Display Modes
  #1  
Old 06-08-2008, 10:31 AM
steadicamop's Avatar
steadicamop steadicamop is offline
 
Join Date: Jul 2004
Location: Lancashire, UK
Posts: 379
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Creating a line break after x images

Hey all,

I'm not too clued up on whiles -- had a look but it messed up pretty early on.

Basically what I need to do is put this code :
PHP Code:
eval('$eic_imagebits .= "' fetch_template('eic_imagebits') . '";'); 
inside a while loop that will count a number of images over (definined the admincp, but for example, say 5) - so when it reaches 5, it will insert a line break and start and the next line.

There are two variables set here, one is the total number of images to be used ($number_of_images_to_show) and then how many across it should go ($numx).

Hope someone can explain this one for me!

TIA

Jason
Reply With Quote
  #2  
Old 06-08-2008, 02:25 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Are you going to be fetching the images (or image data) from a database? If so you just use it as a standard while() loop. Like this:

PHP Code:
$counter 0// Starting counter
while($imgstuff $db->fetch_array($imgquery))
{
    eval(
'$eic_imagebits .= "' fetch_template('eic_imagebits') . '";'); 
    
// ...
    
if($counter == 4// That counts as 5 images 
    
{
        
$eic_imagebits .= '<br />'// Insert Line Break
        
$counter 0// Reset counter
    
}
    else
    {
        
$counter++; // Increment counter
    
}

Reply With Quote
  #3  
Old 06-08-2008, 03:33 PM
Dismounted's Avatar
Dismounted Dismounted is offline
 
Join Date: Jun 2005
Location: Melbourne, Australia
Posts: 15,047
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Pre-incrementing is faster than post-incrementing - so pre-increment.
PHP Code:
    if($counter == 4// That counts as 5 images 
    
{
        
$eic_imagebits .= '<br />'// Insert Line Break
        
$counter 0// Reset counter
    
}
    else
    {
        ++
$counter// Increment counter
    

Reply With Quote
  #4  
Old 06-08-2008, 03:55 PM
steadicamop's Avatar
steadicamop steadicamop is offline
 
Join Date: Jul 2004
Location: Lancashire, UK
Posts: 379
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Opserty View Post
Are you going to be fetching the images (or image data) from a database? If so you just use it as a standard while() loop. Like this:

PHP Code:
$counter 0// Starting counter
while($imgstuff $db->fetch_array($imgquery))
{
    eval(
'$eic_imagebits .= "' fetch_template('eic_imagebits') . '";'); 
    
// ...
    
if($counter == 4// That counts as 5 images 
    
{
        
$eic_imagebits .= '<br />'// Insert Line Break
        
$counter 0// Reset counter
    
}
    else
    {
        
$counter++; // Increment counter
    
}

There's nothing being pulled from the database, just pulling from two settings in the admincp - the total number of images to be shown and the number of images to display per line ($number_of_images_to_show) and ($numx).

--------------- Added [DATE]1212946799[/DATE] at [TIME]1212946799[/TIME] ---------------

Basically put this is what I'm aiming for it to do .. trust me, this is in basic terms!

If there are 10 images to be displayed and every 5 images, it needs to insert a line break.

So it counts out 5 images, then inserts a line break, to display the final 5 images.

Both the amount of images and the number per line are defined in the admincp.
Reply With Quote
  #5  
Old 06-08-2008, 04:52 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

What are you "cycling" through then? Are you going through and array or what? (where are the 10 images - or their associated data - coming from basically)
Reply With Quote
  #6  
Old 06-08-2008, 05:06 PM
steadicamop's Avatar
steadicamop steadicamop is offline
 
Join Date: Jul 2004
Location: Lancashire, UK
Posts: 379
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Here is the entire code, it's outputting an image to a template ($imagebits), but I've discovered an issue with one particular browser, usually it wraps the images around when hits the edge, but this one won't, so I need to add a system where the number of images on a line can be chosen.

PHP Code:
if ($vbulletin->options['eic_enable'])
{
    
session_start();
    if (!isset(
$_SESSION['secure'])) {
        if (!
$_GET['clicked']) {
            
$amount_of_images_to_show $vbulletin->options['eic_numimgs'];
          
$numx $vbulletin->options['eic_numx'];
            
$answer_position rand(1,$amount_of_images_to_show);
            
$d dir($vbulletin->options['eic_imgpath']);
            while (
false !== ($entry $d->read())) 
                if (
substr($entry,-4) == '.jpg')
                    
$answers[] .= $entry
    
            
$amount count($answers);
            
$the_answer rand(1,$amount);
            for(
$i=1;$i<=$amount_of_images_to_show;$i++) {
                if (
$answer_position == $i$show_image[] = $the_answer;
                
$tmp rand(1,$amount);
                while (
$tmp == $the_answer || (is_array($show_image) && in_array($tmp,$show_image))) $tmp rand(1,$amount);
                
$show_image[] = $tmp;
                    eval(
'$eic_imagebits .= "' fetch_template('eic_imagebits') . '";');
            }
            if (
$answer_position == $i$show_image[] = $the_answer;
            
$d->close();
    
            
$_SESSION['answer_position'] = $answer_position;
            
$_SESSION['show_images'] = $show_image;
            
$question substr($answers[$the_answer-1],0,-4);
        }
        else {
            if (
$_GET['clicked'] == $_SESSION['answer_position']) $_SESSION['secure'] = 1;
            else 
$badchoice 1;
        }
        if (!isset(
$_SESSION['secure'])) {
            eval(
'$navbar = "' fetch_template('navbar') . '";');
            eval(
'print_output("' fetch_template('eic_main_template') . '");');
        }
    }

This chunk of code eval('$eic_imagebits .= "' . fetch_template('eic_imagebits') . '";'); is the one that needs to loop around as many times as chosen in the admincp (for example, say 5) then a break inserted to put the rest of the images below.

Cheers

Jason

--------------- Added [DATE]1212948569[/DATE] at [TIME]1212948569[/TIME] ---------------

I should have said that there is an outside script - show.php which creates the images, but it's all called back into this plugin - and is finally output in the $eic_imagebits template
Reply With Quote
  #7  
Old 06-08-2008, 05:36 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Try avoid using the shortcut if syntax:
PHP Code:
if($blabla)
    
$dobla 'Skf';
// ------------------
// Generally preferred:
if($blabla)
{
    
$dobla 'Skf';

It is easier for others to read your code then...

Anyway, after your template eval() you could add something like:
PHP Code:
// If counter is a multiple of 5 then execute this code...
if(($i 5) == 0))
{
   
$eic_imagebits .= '<br />';

Reply With Quote
  #8  
Old 06-08-2008, 05:49 PM
steadicamop's Avatar
steadicamop steadicamop is offline
 
Join Date: Jul 2004
Location: Lancashire, UK
Posts: 379
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

I found out that it wouldn't work correctly with certain curly brackets it ... so they were removed, hence why they're not there.

I've tried the code you suggested after the eval() and it doesn't work, the plugin fails to load and executes the script after it.

The original code you posted :
PHP Code:
$counter 0// Starting counter
while($imgstuff $db->fetch_array($imgquery))
{
    eval(
'$eic_imagebits .= "' fetch_template('eic_imagebits') . '";'); 
    
// ...
    
if($counter == 4// That counts as 5 images 
    
{
        
$eic_imagebits .= '<br />'// Insert Line Break
        
$counter 0// Reset counter
    
}
    else
    {
        
$counter++; // Increment counter
    
}

Looks like it should be right, but the one thing I couldn't translate is while() statement - should it look more like this?

PHP Code:
while($numx => $amount_of_images_to_show
To me that logically says, if the number of images to be display per line is less than the total number of images to be shown, then put them on the same line ... when it reaches $numx (eg 5), then insert the link break.

Hopefully you can follow my thinking! I thought this would be fairly straightfoward but as with everything I do, proving a headache!!

Cheers,

Jason
Reply With Quote
  #9  
Old 06-08-2008, 06:33 PM
Opserty Opserty is offline
 
Join Date: Apr 2007
Posts: 4,103
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Curly braces won't affect the code execution unless you have not matched them up correctly. They are there to make code more readable.

Why did you insert the code I gave in my first post? (I suggested different code in my second post...)

Try this:
PHP Code:
if ($vbulletin->options['eic_enable'])
{
    
session_start();
    if (!isset(
$_SESSION['secure']))
    {
        if (!
$_GET['clicked']) 
        {
            
$amount_of_images_to_show $vbulletin->options['eic_numimgs'];
              
$numx $vbulletin->options['eic_numx'];
            
$answer_position rand(1,$amount_of_images_to_show);
            
$d dir($vbulletin->options['eic_imgpath']);
            
            while (
false !== ($entry $d->read()))
            {
                if (
substr($entry,-4) == '.jpg')
                {
                    
$answers[] .= $entry
                }
            }
    
            
$amount count($answers);
            
$the_answer rand(1,$amount);
            
            for(
$i=1;$i<=$amount_of_images_to_show;$i++) 
            {
                if (
$answer_position == $i
                {
                     
$show_image[] = $the_answer;
                }
                
                
$tmp rand(1,$amount);
                
                while (
$tmp == $the_answer || (is_array($show_image) && in_array($tmp,$show_image))) 
                {
                    
$tmp rand(1,$amount);
                }
                
                
$show_image[] = $tmp;
                
                eval(
'$eic_imagebits .= "' fetch_template('eic_imagebits') . '";');
                
/** ---------------------
                
                // If counter is a multiple of 5 then execute this code...
                if(($i % 5) == 0))
                {
                   $eic_imagebits .= '<br />';
                }  
                -------------- */
                
            
}
            if (
$answer_position == $i
            {
                 
$show_image[] = $the_answer;
            }
            
$d->close();
    
            
$_SESSION['answer_position'] = $answer_position;
            
$_SESSION['show_images'] = $show_image;
            
$question substr($answers[$the_answer-1],0,-4);
        }
        else 
        {
            if (
$_GET['clicked'] == $_SESSION['answer_position']) 
            {
                 
$_SESSION['secure'] = 1;
            }
            else
            {
                 
$badchoice 1;
            }
        }
        if (!isset(
$_SESSION['secure'])) 
        {
            eval(
'$navbar = "' fetch_template('navbar') . '";');
            eval(
'print_output("' fetch_template('eic_main_template') . '");');
        }
    }

I've commented out my code to highlight it to you, uncomment it and test it out.
Reply With Quote
  #10  
Old 06-08-2008, 06:43 PM
steadicamop's Avatar
steadicamop steadicamop is offline
 
Join Date: Jul 2004
Location: Lancashire, UK
Posts: 379
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Believe it or not, but using that code, it doesn't work. When I paste in my original code - it works fine!

I will admit my brother in law did code most of this, he's a LOT more clued up with php than I am .... but he explained about the curly braces and why they weren't there, I understand with them there they make the code easier to understand but on this lot, they need to be missing for the code to execute.

Forgot to mention, that code I tried was with the extra part still commented out.

Thanks for your help on this, I am extremely grateful.

Jason
Reply With Quote
Reply

Thread Tools
Display Modes

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

Forum Jump


All times are GMT. The time now is 11:49 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04869 seconds
  • Memory Usage 2,340KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (10)bbcode_php
  • (1)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete