Jump to content

php/html help


black_pearl
Go to solution Solved by scootstah,

Recommended Posts

hey guys i am pretty new to html and php and i have a little problem

 

here is the code : 

 

<div id="gallery" align="center">
    <div class="thumbnails">
        <?php
            for ($i = 1; $i <= $total_photos; $i++){ ?>
                <img onmouseover="preview.src=<?php echo $car[0]->photo . $i . '()';?>.src"  src="<?php echo $car[0]->photo1()?>" alt=""/>
       <?php } ?>
 
</div><br/>
 
--------------------------------------------------------------------
 
when i do  : <img onmouseover="preview.src=<?php echo $car[0]->photo1()?>.src"  src="<?php echo $car[0]->photo1()?>" alt=""/>
it works fine gives me the photo1 from the database but i want to get all the other photos as i tried on the code above : and i have tried so many things but cant come up with the right prob missing something a rule or something like that
 
if anyone can help out please :)
thanks
 
Link to comment
Share on other sites

Is it perhaps that this

<img onmouseover="preview.src=<?php echo $car[0]->photo . $i . '()';?>.src"  src="<?php echo $car[0]->photo1()?>" alt=""/>

Needs to be changed to this

<img onmouseover="preview.src=<?php echo $car[0]->photo . $i . '()';?>.src"  src="<?php echo $car[0]->photo . $i . '()'; ?>" alt=""/>
Link to comment
Share on other sites

hey cyberRobot tyvrm for replying :)

 

yes i have already tried this one out and it gives me this error : http://prntscr.com/7vas9m

 

when i do $car->photo . $1 . '()'   i dont know why it just take $car->photo and thats why i get error coz there is nothing called like photo on the db or anywhere else and it also says it in the error  : undefined Car::$photo

Edited by black_pearl
Link to comment
Share on other sites

Hmm...the code seems to work for me when I try the following:

<?php
class testClass {
    public function photo1() {
        print '<div>In photo1</div>';
    }
 
    public function photo2() {
        print '<div>In photo2</div>';
    }
 
    public function photo3() {
        print '<div>In photo3</div>';
    }
}
 
$car = array();
$car[] = new testClass;
$total_photos = 3;
 
for ($i = 1; $i <= $total_photos; $i++) {
    echo call_user_func(array($car[0], "photo$i"));
}
?>
 
 
Do you know which version of PHP you're using? Is PHP set to show all errors and warnings? Note that you can add the following to the top of your script during the debugging process:
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
 
Link to comment
Share on other sites

idk thats the way i learned it

but it works fine when i do it like this : echo $car[0]->photo1() or echo $car[0]->photo2() or echo $car[0]->photo3() ...

but idk how many pics the user will add (max is 8 photos per car) and i want to use the " for "

the problem is idk how to get it right echo $car[0]->photo . $i . "()"    this doesn't work it only does echo $car[0]->photo  thus gives me error i want to be able to send this echo $car[0]->photo1() with the $i

Link to comment
Share on other sites

  • Solution

This is the correct syntax for what you're trying to do:

<img onmouseover="preview.src=<?php echo $car[0]->{'photo' . $i}();?>.src"  src="<?php echo $car[0]->{'photo' . $i}()?>" alt=""/>
But, I agree with what other's have said: that's a really wacky way to go about things.
Link to comment
Share on other sites

For what it's worth, many of the methods could be replaced with a single method that returns the requested variable. Here's a quick example of the direction you could go:

<?php
//REPORT ALL PHP ERRORS
error_reporting(E_ALL);
ini_set('display_errors', 1);
 
 
class testClass {
    private $_portes = 'Portes';
    private $_photo1 = 'Photo1';
    private $_photo2 = 'Photo2';
    private $_photo3 = 'Photo3';
 
 
    public function getValue($variableName) {
        if(isset($this->$variableName)) {
           return $this->$variableName;
        }
    }
}
 
 
$car = new testClass;
echo '<div>' . $car->getValue('_photo1') . '</div>';
echo '<div>' . $car->getValue('_portes') . '</div>';
$i = 2;
echo '<div>' . $car->getValue("_photo$i") . '</div>';

?>

Link to comment
Share on other sites

I'd go for something like this:

class testClass
{
private $photos;

public function setPhotos(array $photos)
{
$this->photos = $photos;
}

public function getPhotos()
{
return $this->photos;
}
}

$car = new testClass();
$car->setPhotos(array('photo1.jog', 'photo2.jpg', 'photo3.jpg'));

foreach ($car->getPhotos() as $photo) {
echo '<img src="' . $photo . '" />';
}
Link to comment
Share on other sites

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.