View Full Version : Creating a line break after x images
steadicamop
06-08-2008, 10:31 AM
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 :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
Opserty
06-08-2008, 02:25 PM
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:
$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
}
}
Dismounted
06-08-2008, 03:33 PM
Pre-incrementing is faster than post-incrementing - so pre-increment. :)
if($counter == 4) // That counts as 5 images
{
$eic_imagebits .= '<br />'; // Insert Line Break
$counter = 0; // Reset counter
}
else
{
++$counter; // Increment counter
}
steadicamop
06-08-2008, 03:55 PM
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:
$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 1212946799 at 1212946799 ---------------
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.
Opserty
06-08-2008, 04:52 PM
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)
steadicamop
06-08-2008, 05:06 PM
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.
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 1212948569 at 1212948569 ---------------
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
Opserty
06-08-2008, 05:36 PM
Try avoid using the shortcut if syntax:
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:
// If counter is a multiple of 5 then execute this code...
if(($i % 5) == 0))
{
$eic_imagebits .= '<br />';
}
steadicamop
06-08-2008, 05:49 PM
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 :
$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?
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
Opserty
06-08-2008, 06:33 PM
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:
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.
steadicamop
06-08-2008, 06:43 PM
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
Opserty
06-08-2008, 06:58 PM
Woah! You need to uncomment my code... otherwise its not going to be executed is it? :p
steadicamop
06-08-2008, 07:20 PM
I've tried it again, and again, with the code uncommented this time, it still bypasses and executes the part after it (basically, it doesn't show the images, just goes straight to the next step, as if you had completed the image selection).
Would it be of any use if I gave you access to my admin control panel so you can test this for yourself?
Cheers
Jason
Opserty
06-08-2008, 07:22 PM
I just noticed an extra bracket:
if(($i % 5) == 0))
Remove the bracket in red.
steadicamop
06-08-2008, 07:40 PM
Thank you!! Thank you!!
Works perfectly now ... like a charm, I had a few issues of my own ... wrong variables, but ironed those out and now it's perfect, thank you again, I wil make sure your name is credited for your assistance!
Cheers
Jason
--------------- Added 1212958533 at 1212958533 ---------------
Now I just have to figure out how to thread a damn sewing machine!!!
Dismounted
06-09-2008, 03:26 AM
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.
Curly braces will never (yes - I said it) stop code executing - provided they are matched up properly.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.