SitePoint Sponsor

User Tag List

Results 1 to 19 of 19
  1. #1
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    how to apply .class to my php code

    Hi all

    Just a basic fetch results displaying images and creating a new <p> after every 4th image, but I need to apply css class .last to every 4th image, how do I add it?

    Code PHP:
    $result = @mysql_query($sql) or die('Error: ' . mysql_error());
    				for ($i = 0;
    				$row = mysql_fetch_array ($result); $i++) {
        			if(($i != 0) and (($i &#37; 4) == 0))
        			echo'<p></p>';

    Suggestions?
    Thanks
    The more you learn.... the more you learn there is more to learn.

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Your code example looks a bit off. Anyway, assuming it works as it is now, it's as simple as
    PHP Code:
    echo '<p class=\'last\'></p>'

  3. #3
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to apply it to the 4th <img> tag not the <p>

    Your code example looks a bit off.
    Whats up? it does work

    Thanks

    update my img output
    Code PHP:
    echo '<a href="images/big/' . $row['pid'] . '.jpg"><img src="images/small/' . $row['pid'] . '.jpg"></a>';}?>
    The more you learn.... the more you learn there is more to learn.

  4. #4
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It will be necessary to know how the image tags are being generated. In other words the one line of code you have posted is not a good representation of the code environment. There are many ways of doing what you are after but in order to give you the right answer people will need to see the code block and not one line.

    For starters you could set a counter and check the result of division by 4. But like I said this may or may not be useful depending on how you are generating the tags.

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    He's got the counter (tho you should start with $i=1 as 0,1,2,3,4 <-- 4 is the fifth element).

    <img> is the same as <p>
    eg.
    $class = '';
    if($i&#37;4 == 0) { $class = 'last'; }
    echo '<img class="'.$class.'">';

  6. #6
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys, should of posted the full code

    full code - what do I need to add?

    Code PHP:
    $sql = "SELECT pid, views FROM photos WHERE name='" .mysql_real_escape_string($_GET['name'])."'";
    				$result = @mysql_query($sql) or die('Error: ' . mysql_error());
    				for ($i = 0;
    				$row = mysql_fetch_array ($result, MYSQL_ASSOC); $i++) {
        			if(($i != 0) and (($i &#37; 4) == 0))
        			echo'<p></p>';
    				echo '<a href="images/big/' . $row['pid'] . '.jpg"><img src="images/small/' . $row['pid'] . '.jpg"></a>';}

    Cheers
    The more you learn.... the more you learn there is more to learn.

  7. #7
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could start by setting the counter to an initial value of 1 or you could do the increment before the mod check.

    Quote Originally Posted by hash View Post
    He's got the counter (tho you should start with $i=1 as 0,1,2,3,4 <-- 4 is the fifth element).

    <img> is the same as <p>
    eg.
    $class = '';
    if($i%4 == 0) { $class = 'last'; }
    echo '<img class="'.$class.'">';

  8. #8
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks semantic7 - php not one of my strongest languages could you show me how to fit it all together?

    Thanks
    The more you learn.... the more you learn there is more to learn.

  9. #9
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should help...

    Code:
    <?php
    
    $sql = "SELECT pid, views FROM photos WHERE name='" .mysql_real_escape_string($_GET['name'])."'";
    
    $result = @mysql_query($sql) or die('Error: ' . mysql_error());
    
    $count = 0;
    $class = '';
    
    for ($i = 0; $row = mysql_fetch_array ($result, MYSQL_ASSOC); $i++)
    {
    	$count++;
    	if ($count%4 == 0)
    	{
    		$class = ' class="last"';
    	} else {
    		$class = '';
    	}
    	if(($i != 0) and (($i % 4) == 0))
    	echo'<p></p>';
    	echo '<a href="images/big/' . $row['pid'] . '.jpg"><img'.$class.' src="images/small/' . $row['pid'] . '.jpg"></a>';
    }
    
    ?>

  10. #10
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doesn't like something, no error but no result?

    Example from my html output and no .last class is showing:

    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <p></p>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>

    it should be:

    <p>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo.. class="last"></a>
    </p>

    and so on..
    The more you learn.... the more you learn there is more to learn.

  11. #11
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok I see where the problem is... Try this...

    Code:
    <?php
    
    $sql = "SELECT pid, views FROM photos WHERE name='" .mysql_real_escape_string($_GET['name'])."'";
    
    $result = @mysql_query($sql) or die('Error: ' . mysql_error());
    
    $class = '';
    
    for ($i = 0; $row = mysql_fetch_array ($result, MYSQL_ASSOC); $i++)
    {
    	if (($i+1)&#37;4 == 0)
    	{
    		$class = ' class="last"';
    	} else {
    		$class = '';
    	}
    	if(($i != 0) and (($i % 4) == 0))
    	echo'</p><p>';
    	echo '<a href="images/big/' . $row['pid'] . '.jpg"><img'.$class.' src="images/small/' . $row['pid'] . '.jpg"></a>';
    }
    
    ?>

  12. #12
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    half way there!

    Ok now it shows

    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img class="last" src="images/.. ></a>
    <p></p>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img class="last" src="images/.. ></a>

    should be:
    <p>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img class="last" src="images/.. ></a>
    </p>

    and instead of the <img class="last" src="images/.. > it should really be applied to the 4th <a href="images/.." class="last"> but either way will work, I just want to keep my code well structured and follow my other css rules which are applied to my a:link img tags.

    ??
    thanks
    The more you learn.... the more you learn there is more to learn.

  13. #13
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    If you want the class attribute in the link that contains the image tag, move the
    ' . $class . '
    from the img tag to the a tag.

    Try an echo '<p>' before the for loop and an echo '</p>' after it -- leaving the inner echo as '</p><p>'

  14. #14
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do it... well lets hope so

    Code:
    <?php
    
    $sql = "SELECT pid, views FROM photos WHERE name='" .mysql_real_escape_string($_GET['name'])."'";
    
    $result = @mysql_query($sql) or die('Error: ' . mysql_error());
    
    $ptag = $class = '';
    
    echo '<p>';
    
    for ($i = 0; $row = mysql_fetch_array ($result, MYSQL_ASSOC); $i++)
    {
    	if (($i+1)&#37;4 == 0)
    	{
    		$class = ' class="last"';
    		if ($i == mysql_num_rows($result))
    		{
    			$ptag = '</p>';
    		}
    		else
    		{
    			$ptag = '</p><p>';
    		}
    	}
    	else
    	{
    		$class = '';
    		$ptag = '';
    	}
    	echo '<a'.$class.' href="images/big/' . $row['pid'] . '.jpg"><img src="images/small/' . $row['pid'] . '.jpg"></a>';
    	echo $ptag;
    }
    
    ?>

  15. #15
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks guys one last problem

    class is now applied to the 4th link, <p> is present with closing tag </p> works great, but after all the images have shown it places a empty start <p>

    <p>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a class="last" href="images/.."><img photo ></a>
    </p><p>

    should be:

    <p>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a href="images/.."><img photo..</a>
    <a class="last" href="images/.."><img photo..</a>
    </p>

    ??
    The more you learn.... the more you learn there is more to learn.

  16. #16
    SitePoint Addict
    Join Date
    Apr 2007
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    replace "if ($i == mysql_num_rows($result))" with "if ($i == mysql_num_rows($result) - 1)"

  17. #17
    SitePoint Wizard
    Join Date
    Dec 2005
    Posts
    1,738
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks semantic7 works great now!

    Thanks again guys, and if you have a minute would it be possible to display a few comments on the code so I know exactly what's doing what?

    The more you learn.... the more you learn there is more to learn.

  18. #18
    SitePoint Wizard
    Join Date
    Nov 2005
    Posts
    1,191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    --

  19. #19
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,290
    Mentioned
    198 Post(s)
    Tagged
    3 Thread(s)
    Going by your sig
    Quote Originally Posted by computerbarry
    The more you learn.... the more you learn there is more to learn.
    you do want to learn.

    Although the code provided by others may have been meant to be helpful, unfortunately it isn't always the best way to help someone learn.

    Although the code examples "work", they can be improved upon some, and if you had worked through the process yourself through discussion rather than have had code given to you, you would understand things better and perhaps see ways to tweak it to better suit your use of it.

    So, the learning process may be more difficult for you now as you need to "learn backwards" as it were, but this should help

    Code:
    <?php
    /* http://www.php.net/mysql_real_escape_string */
    $sql = "SELECT pid, views FROM photos WHERE name='" . mysql_real_escape_string($_GET['name']). "'";
    /* http://www.php.net/mysql_query
    * http://www.php.net/die
    * http://www.php.net/mysql_error */
    $result = @mysql_query($sql) or die('Error: ' . mysql_error());
    
    $ptag = $class = '';
    /* http://www.php.net/echo */
    echo '<p>';
    /* http://www.php.net/for
    * http://www.php.net/mysql_fetch_array */
    for ($i = 0; $row = mysql_fetch_array ($result, MYSQL_ASSOC); $i++)
    {
    /* http://www.php.net/if
    * http://www.php.net/language.operators.arithmetic */
    	if (($i+1)&#37;4 == 0)
    	{
    		$class = ' class="last"';
    /* http://www.php.net/mysql_num_rows */
    		if ($i == mysql_num_rows($result) - 1)
    		{
    			$ptag = '</p>';
    		}
    		else
    		{
    			$ptag = '</p><p>';
    		}
    	}
    	else
    	{
    		$class = '';
    		$ptag = '';
    	}
    	echo '<a'.$class.' href="images/big/' . $row['pid'] . '.jpg"><img src="images/small/' . $row['pid'] . '.jpg"></a>';
    	echo $ptag;
    }
    ?>
    And if there's anything not answered by the documentation that you don't understand and want to discuss, please ask.
    Last edited by Mittineague; May 27, 2009 at 07:35. Reason: formatting for links


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •