Jump to content

php/html help


black_pearl

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
https://forums.phpfreaks.com/topic/297404-phphtml-help/
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
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516963
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

Link to comment
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516964
Share on other sites

It looks like you can use the call_user_func() function to dynamically name your class methods. You could try something like the following:

<img onmouseover="preview.src=<?php echo call_user_func(array($car[0], "photo$i")); ?>.src"  src="<?php echo call_user_func(array($car[0], "photo$i")); ?>" alt=""/>
Link to comment
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516968
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
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516972
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
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516979
Share on other sites

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
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516981
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
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516985
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
https://forums.phpfreaks.com/topic/297404-phphtml-help/#findComment-1516990
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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