black_pearl Posted July 21, 2015 Share Posted July 21, 2015 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 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 21, 2015 Share Posted July 21, 2015 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=""/> Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 (edited) 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 July 21, 2015 by black_pearl Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 21, 2015 Share Posted July 21, 2015 If that doesn't work...which it probably won't, where does $car come from. Are you able to show the code where photo1() is defined? I imagine that it comes from a class definition. If that's correct, does the class have multiple methods like photo1(), photo2(), etc.? Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 yes this is it and i am trying to do $car[0]->photo1(), $car[0]->photo2() , $car[0]->photo3() ,$car[0]->photo4() .. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 21, 2015 Share Posted July 21, 2015 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=""/> Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 just tried it gives me empty items, i just copied the code u posted did i have to change something else anywhere ? Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 if u dont mind we can do a teamviewer session ? would be easier ah shi i dont have a mic Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 21, 2015 Share Posted July 21, 2015 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); Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted July 21, 2015 Share Posted July 21, 2015 @black_pearl Why does your class have functions photo1(), photo2(), photo3() etc defined? Why not just have a photo function defined and pass the id of the photo to the function? 1 Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 21, 2015 Share Posted July 21, 2015 Are you able to show what the photo1(), photo2(), photo3() functions look like? Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 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 Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 they r just returns : http://prntscr.com/7vbf1c Quote Link to comment Share on other sites More sharing options...
Solution scootstah Posted July 21, 2015 Solution Share Posted July 21, 2015 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. Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 thank you very much scootstah and cyberRobot for the help it works now i tried the code u gave scootstah and it works but u said its a wacky way, how can i improve it how else can i do it ? i have learned that way thats why went for it Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted July 21, 2015 Share Posted July 21, 2015 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>'; ?> Quote Link to comment Share on other sites More sharing options...
scootstah Posted July 21, 2015 Share Posted July 21, 2015 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 . '" />'; } Quote Link to comment Share on other sites More sharing options...
black_pearl Posted July 21, 2015 Author Share Posted July 21, 2015 wow that way would of save me a lot of time all the returns i wrote could be done with one function thanks to you guys now i know this way Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.