Using Printf Command - Width and Height problem

7 replies
Hey Warriors,

I am having a problem specifying the width and height using the printf command.

The code is as below:

Code:
printf('
						<a href="'.get_term_link( $term ).'" style="color:%s;" data-javo-category="%s">
							<img src="%s" style="width:300px; height:200px;">
							<span style="font-size:%s'.'px;">%s</span>
							<div class="categories-wrap-shadow"></div>
							<div class="inner-meta %s">
<!--								<div>%s</div> -->
<!--								<small>%s</small> -->
								<div class="cat-title">'.$term->name.'</div>
								<div class="cat-des">'.$term->description.'</div>

				<?php endif; ?>
								</div>
						</a>'
						, $title_text_color
						, $term->term_id
						, $featured
						, $category_size
						, $term->name
						, ( $display_item_count == 'hide' ? 'hidden' : '' )
						, $term->name
						, javo_get_count_in_taxonomy( $term->term_id )
					);

When I perform speed analysis using GTMetrix, it says that "image(s) are missing width and/or height attributes." If I put it as follows:

Code:
width="300" and height="200"
then I get printf function error saying too few arguments in printf function.

If I delete the width and height specifications from here and put it in the stylesheet, still it does have the same warning in GTMetrix

Please help me to correctly specify the size in this command so that it doesn't give warnings in GTMetrix
#command #height #printf #problem #width
  • Profile picture of the author ripsnorta2
    I assume you're doing this in PHP.

    You are trying to embed some special characters (double quotes) in a string. C languages start and end strings with quotes. If you put a quote in the middle, the language sees that as the end of the string. You need to escape the special character so that the compiler/interpreter sees it as a literal rather than the end of the string.

    Try escaping the special characters in the string using the \ character. So to escape a double quote it would look like: "width=\"300\""

    See here for more info on printf: PHP: printf - Manual
    {{ DiscussionBoard.errors[9819223].message }}
    • Profile picture of the author keyuria
      Originally Posted by ripsnorta2 View Post

      I assume you're doing this in PHP.

      You are trying to embed some special characters (double quotes) in a string. C languages start and end strings with quotes. If you put a quote in the middle, the language sees that as the end of the string. You need to escape the special character so that the compiler/interpreter sees it as a literal rather than the end of the string.

      Try escaping the special characters in the string using the character. So to escape a double quote it would look like: "width="300""

      See here for more info on printf: PHP: printf - Manual
      Thank you for your suggestion but it doesn't work.

      I put the code as below:
      <img src="%s" "width=\"300\"" "height=\"200\"">
      {{ DiscussionBoard.errors[9821434].message }}
      • Profile picture of the author geekSoftware
        Originally Posted by keyuria View Post

        Thank you for your suggestion but it doesn't work.

        I put the code as below:
        <img src="%s" "width="300"" "height="200"">
        Change this line:
        HTML Code:
        <img src="%s" style="width:300px; height:200px;">
        To:
        HTML Code:
        <img src="%s" width="300px" height="200px" />
        This should work, let me know.
        {{ DiscussionBoard.errors[9821755].message }}
      • Profile picture of the author ripsnorta2
        Can I suggest the divide and conquer approach?

        Zenoth is right, the code is very messy, which makes it much harder to debug.

        I'd build up the string a bit at a time initially by putting each section in its own variable and then adding them together.

        For example
        $img = "<img src=\"/image.jpg\" style=\"width:300px; height:200px;\" />"
        $href = "<a href=\"/url">".$img."</a>"
        printf($href)
        That way when you're debugging you can check the various parts of the string for correctness as you build it up. When you're confident that it's right, then you can recombine it.


        BTW, I noticed when looking through your original code again, that you didn't terminate the img tag with a /> or </img> you should probably change it to <img src="%s" style="width:300px; height:200px;" />
        {{ DiscussionBoard.errors[9826703].message }}
  • Profile picture of the author keyuria
    Nope, it didn't work either.

    When I put that code, it doesn't recognize the command and it goes to the default image size.
    {{ DiscussionBoard.errors[9822795].message }}
    • Profile picture of the author geekSoftware
      Originally Posted by keyuria View Post

      Nope, it didn't work either.

      When I put that code, it doesn't recognize the command and it goes to the default image size.
      Try to post all code you have in that file so we can have a better look, the problem could be somewhere else, not right there. I know that the code I gave to you must work, because I have tested that before posted reply.
      {{ DiscussionBoard.errors[9823007].message }}
      • Profile picture of the author Zenoth
        You have an <?php endif; ?> there, but I don't see where the if starts.

        I think that if statement will end your printf at some point and that's the reason you are getting the error that says that the printf function has too few arguments.

        Also, if all the code is inside <?php ?> tags, why the endif has PHP tags around it?

        The code looks a little messy and hard to read as it is. You can close the PHP tags and use simple HTML+PHP inside a PHP file if the code is too large (for a better readability) instead the printf.
        {{ DiscussionBoard.errors[9824106].message }}

Trending Topics