sitorush Posted May 22, 2007 Share Posted May 22, 2007 Hello friends, i have problem with sprintf $image = '<div align="center"><a href="images/'.$gambar.'/%%s" target="_blank"><img src="'.$phpThumbBase.'./images/'.$gambar.'/%s&aoe=1&w='.$width.'" alt=""></div>'; in the loop... echo '<td>'.(isset($chart['tampil'][$row][$col])? sprintf($image,$chart['tampil'][$row][$col]) : '').'</td>'; sprintf wont print $image if i put %s twice it says Warning: sprintf() [function.sprintf]: Too few arguments in C:\wamp\www\phpthumb\incl\catalog8.php on line 31 but if i put %%s in front it will work but can't make as link (bad request). can you help me friends... thanks:) tom Quote Link to comment https://forums.phpfreaks.com/topic/52583-solved-problem-with-sprintf/ Share on other sites More sharing options...
trq Posted May 23, 2007 Share Posted May 23, 2007 What are you trying to do exactly? Your code is a mess. Your only passing one parameter to sprintf yet you have several (attempts at) placeholders. With all those variables in the string your passing to sprintf I'm not sure you understand what sprintf is even for. Take a look at the man. ps; please use the [ code ] [/ code ] tags (without the space), it makes code MUCH easier to read. Quote Link to comment https://forums.phpfreaks.com/topic/52583-solved-problem-with-sprintf/#findComment-259473 Share on other sites More sharing options...
btherl Posted May 23, 2007 Share Posted May 23, 2007 Here's an example $i = 0; while ($i < 10) { $str = sprintf("[%s] %s\n", $i, "This is row $i"); print $str; $i++; } The % placeholders match the order of the arguments. So the first one is replaced by $i, and the second replaced by "This is row $i". It's usually a bad idea to mix "." style and sprintf() style. They are two different ways to accomplish the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/52583-solved-problem-with-sprintf/#findComment-259507 Share on other sites More sharing options...
trq Posted May 23, 2007 Share Posted May 23, 2007 I would even consider that a bad example. It should be used more like.... $i = 0; while ($i < 10) { $str = sprintf("[%s] This is row %s\n", $i, $i); print $str; $i++; } Quote Link to comment https://forums.phpfreaks.com/topic/52583-solved-problem-with-sprintf/#findComment-259521 Share on other sites More sharing options...
btherl Posted May 23, 2007 Share Posted May 23, 2007 I was intending to show the difference between using a variable directly, and using a string I think that using "string $var" or "." style in the arguments to sprintf is fine, just not in the format string. Using it in the format string is missing the point of sprintf altogether. Quote Link to comment https://forums.phpfreaks.com/topic/52583-solved-problem-with-sprintf/#findComment-259530 Share on other sites More sharing options...
sitorush Posted May 23, 2007 Author Share Posted May 23, 2007 Now i get the point, $i must twice if %s twice. thankyou very much for your help. sorry the code was messy... Quote Link to comment https://forums.phpfreaks.com/topic/52583-solved-problem-with-sprintf/#findComment-259590 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.