Jump to content

[SOLVED] problem with sprintf


sitorush

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.