Jump to content

Recommended Posts

hi,

 

i need help, i have a code here wherein i will keep on adding whenever i

need to add a new image for the gallery of the website.

 

<? 
/* add $thumbsxxx depedening on how many your image thumbnails */
$thumbs1 = "images/1.gif";
$thumbs2 = "images/2.gif";
$thumbs3 = "images/3.gif";
$thumbs4 = "images/4.gif";
$thumbs5 = "images/5.gif";
$thumbs6 = "images/6.gif";
/* add $thumbsxxx end */

/*add $image_thumbs[""] = ""; depending on how many your large images */
$image_thumbs["$thumbs1"] = "images/1.jpg";
$image_thumbs["$thumbs2"] = "images/2.jpg";
$image_thumbs["$thumbs3"] = "images/3.jpg";
$image_thumbs["$thumbs4"] = "images/4.jpg";
$image_thumbs["$thumbs5"] = "images/5.jpg";
$image_thumbs["$thumbs6"] = "images/6.jpg";
/*add $image_thumbs[""] = ""; end */

/*add $text_thumbs[""] = ""; depending on how many your large images */
$text_thumbs["$thumbs1"] = "images/t1.gif";
$text_thumbs["$thumbs2"] = "images/t2.gif";
$text_thumbs["$thumbs3"] = "images/t3.gif";
$text_thumbs["$thumbs4"] = "images/t4.gif";
$text_thumbs["$thumbs5"] = "images/t5.gif";
$text_thumbs["$thumbs6"] = "images/t6.gif";
/*add $text_thumbs[""] = ""; end */
?>

 

 

is there a way to make this simplier and shorter?

so that i dont need to add line of codes each time i add a new image?

the naming convention of the filename and variables are base on how many the total image files are.

i know what logic to use already but i dont know how to code it

it something like this

 

$total number of images = xx;

/* add $thumbsxxx depedening on how many your image thumbnails */
$thumbsxx = "images/xx.gif";
/* add $thumbsxxx end */
/*add $image_thumbs[""] = ""; depending on how many your large images */
$image_thumbs["$thumbsxx"] = "images/xx.jpg";
/*add $image_thumbs[""] = ""; end */
/*add $text_thumbs[""] = ""; depending on how many your large images */
$text_thumbs["$thumbsxx"] = "images/txx.gif";
/*add $text_thumbs[""] = ""; end */

 

is there a way to get a code in such i way that i will just replace the xx from $total number of images

so that i wont need to add 1 line for each and the output will be something like an array or something?

 

hope you could help me with this.

 

thanks!

 

 

Link to comment
https://forums.phpfreaks.com/topic/74085-solved-need-help-with-my-php-code/
Share on other sites

You could auto-build the array in about half the code.

Then just specify the number ou want to add.

Or you can turn it oop, and easily add new images into the array.

$this->classname->addtoarray('key', 'value');

Then at the same time have it do all the other stuff at the same time as well.

If images are named 1.gif, 2.gif, 3.gif then you could do:

 

$images = 6;

// create image vars
for($i = 1, $j = $images; $i < $j; $i++)
{
    $image_thumbs['images/t' . $i . '.gif'] = 'images/' . $i . '.gif';
}

// display generated images:
echo '<pre>' . print_r($image_thumbs, true) . '</pre>';

hi,

 

i tried this one

<? 
for ($a = 1; $a <= "17"; $a++){
/* add $thumbsxxx depedening on how many your image thumbnails */
$thumbs.$a = "images/".$a.".gif";
/* add $thumbsxxx end */
/*add $image_thumbs[""] = ""; depending on how many your large images */
$image_thumbs["'"."$thumbs".$a."'"] = "images/".$a.".jpg";
/*add $image_thumbs[""] = ""; end */

/*add $text_thumbs[""] = ""; depending on how many your large images */
$text_thumbs["'"."$thumbs".$a."'"] = "images/t".$a.".gif";
/*add $text_thumbs[""] = ""; end */
}
?>

 

but i am getting this error

Notice: Undefined variable: thumbs in /var/www/html/ILEC/portfolio/raffles/gallery.php on line 4

Notice: Undefined variable: thumbs in /var/www/html/ILEC/portfolio/raffles/gallery.php on line 7

Notice: Undefined variable: thumbs in /var/www/html/ILEC/portfolio/raffles/gallery.php on line 11

 

hope you could help me with this.

 

thanks!

// create image vars
for($i = 1; $i < 17; $i++)
{
    ${'thumbs' . $i} = 'images/' . $i . '.gif';

    $image_thumbs[${'thumbs' . $i}] = ${'thumbs' . $i};

    $text_thumbs[${'thumbs' . $i}] = 'images/t' . $i . '.gif';
}

// display generated images:
echo '<pre>' . print_r($image_thumbs, true) . '</pre>';
echo '<pre>' . print_r($text_thumbs, true) . '</pre>';

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.