Jump to content


Photo

PHP & Functions


  • Please log in to reply
6 replies to this topic

#1 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 20 June 2006 - 02:54 PM

Hi,

whenever I include a function in my myscript, it goes all the way at the top of the loop. Is there any way to prevent this from happening?

You can see what I am talking about here: [a href=\"http://informedia.ws/projects/tuts/viewcat.php?act=maincat&id=1\" target=\"_blank\"]http://informedia.ws/projects/tuts/viewcat...ct=maincat&id=1[/a]

The stars are all the way at the top, when they should be right next to "Rating:"

The code is:


 $sql2 = "SELECT * FROM `tutorials` WHERE `catid` = '$id' and `activation` = 1 ORDER BY `id` DESC LIMIT 5";
$result2=mysql_query($sql2);
if ($result) { 
while($rows2=mysql_fetch_array($result2)){

             $id = $rows2['id'];
         $author = $rows2['author'];
    $description = $rows2['description'];
           $name = $rows2['name'];
       $imageurl = $rows2['imageurl'];
            $url = $rows2['url'];
           $url2 = 'viewtut.php?id='.$rows2['id'].'';
    $totalviewed = $rows2['hits'];
           $cat1 = $rows2['catid'];
     $tut_rating = $rows2['rating'];

           $sql3 = "SELECT * FROM `category` WHERE `cat_id` = '$cat1'";
         $result3=mysql_query($sql3);
           $rows3=mysql_fetch_array($result3);
           $cat3 = $rows3['category'];

echo "
    <div class='tutorial'>
      <div class='tutorialtitle'> <span class='style1'><a href='".$url2."'>".$name."</a></span><br />
        Author: <a href='#'>".$author."</a> <br> Rating: <span class='style3'>".tut_stars($tut_rating)."</span></div>
      <img src='".$imageurl."' width='44' height='43' alt='' /><br />".$description."<br />
      <span class='style2'>22nd March 2006 <span class='style3'><a href='#'>Report Tutorial</a></span> <span class='style3'>Total Views: <strong>".$totalviewed."</strong></span> <span class='style3'>Categorgy: <a href='viewcat.php?act=maincat&id=".$cat1."'><strong>".$cat3."</strong></a></span></span></div>";

}
}


Help would be awesome, thanks.

#2 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 03:05 PM

The generated source on your example site:
[!--html--][div class=\'htmltop\']HTML[/div][div class=\'htmlmain\'][!--html1--]<br /><img src='http://informedia.ws/projects/tuts/images/stars/5.png' width='90' height='20'>
<div class='tutorial'>

<div class='tutorialtitle'> <span class='style1'><a href='viewtut.php?id=11'>Mike rocks</a></span><br />
Author: <a href='#'>Mike</a> <br> Rating: <span class='style3'></span></div>
<img src='tutorialchimp.com/images/logo' width='44' height='43' alt='[/span]' /><[span style=\'color:blue\']br />wwwwipiiee<br />
<span class='style2'>22nd March 2006 <span class='style3'><a href='#'>Report Tutorial</a></span> <span class='style3'>Total Views: <strong>0</strong></span> <span class='style3'>Categorgy: <a href='viewcat.php?act=maincat&id=1'><strong>Adobe Photoshop</strong></a></span></span></div><img src='http://informedia.ws/projects/tuts/images/stars/5.png' width='90' height='20'>[!--html2--][/div][!--html3--]
can not be generated from the code you posted. Are you sure the uploaded code is the same as the code you posted?

Ken



#3 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 20 June 2006 - 03:45 PM

Yes, I am 100% sure.

This is the function part:


function tut_stars($tut_rating){


$sql = "SELECT * FROM `tutorials` WHERE `id` = '$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$rating = $rows['rating'];

    if((($tut_rating >= 0)or($tut_rating == 0)) && ($tut_rating <= 0.50)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/0.png' width='90' height='20'>";
    }
    
    
    
    if((($tut_rating  >= 0.50)or($tut_rating == 0.50)) && ($tut_rating <= .99)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/0point5.png' width='90' height='20'>";
    }
    if((($tut_rating  >= 1.00)or($tut_rating == 1.50)) && ($tut_rating <= 1.49)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/1.png' width='90' height='20'>'";
    }
    if((($tut_rating  >= 1.50)or($tut_rating == 1.50)) && ($tut_rating <= 1.99)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/1point5.png' width='90' height='20'>";
    }
    if((($tnew_rating2  >= 2.00)or($tut_rating == 2.00)) && ($tut_rating <= 2.49)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/2.png' width='90' height='20'>";
    }

    if((($tut_rating  >= 2.50)or($tut_rating == 2.50)) && ($tut_rating <= 2.99)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/2point5.png' width='90' height='20'>";
    }

    if((($tut_rating  >= 3.00)or($tut_rating == 3.00)) && ($tut_rating <= 3.49)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/3.png' width='90' height='20'>";
    }

    if((($tut_rating  >= 3.50)or($tut_rating == 3.50)) && ($tut_rating <= 3.99)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/3point5.png' width='90' height='20'>";
    }
    if((($tut_rating  >= 4.00)or($tut_rating == 4.00)) && ($tut_rating <= 4.49)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/4.png' width='90' height='20'>";
    }
    if((($tut_rating  >= 4.50)or($tut_rating == 4.50)) && ($tut_rating <= 4.99)){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/4point5.png' width='90' height='20'>";
    }
    if($tut_rating  == 5.0){
        echo "<img src='http://informedia.ws/projects/tuts/images/stars/5.png' width='90' height='20'>";
    }
} 



#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 03:53 PM

I see your problem. You don't want to echo the string in your function, you want to return it so it can be echoed with the rest of the string.

Ken


#5 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 20 June 2006 - 04:08 PM

So you want me to make the functions like this:


function tut_stars($tut_rating){


$sql = "SELECT * FROM `tutorials` WHERE `id` = '$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
$rating = $rows['rating'];

    if((($tut_rating >= 0)or($tut_rating == 0)) && ($tut_rating <= 0.50)){
       "<img src='http://informedia.ws/projects/tuts/images/stars/0.png' width='90' height='20'>";
    }
    
    
    
    if((($tut_rating  >= 0.50)or($tut_rating == 0.50)) && ($tut_rating <= .99)){
       " <img src='http://informedia.ws/projects/tuts/images/stars/0point5.png' width='90' height='20'>";
    }
    if((($tut_rating  >= 1.00)or($tut_rating == 1.50)) && ($tut_rating <= 1.49)){
       " <img src='http://informedia.ws/projects/tuts/images/stars/1.png' width='90' height='20'>";
    }
    if((($tut_rating  >= 1.50)or($tut_rating == 1.50)) && ($tut_rating <= 1.99)){
       " <img src='http://informedia.ws/projects/tuts/images/stars/1point5.png' width='90' height='20'>";
    }
    if((($tnew_rating2  >= 2.00)or($tut_rating == 2.00)) && ($tut_rating <= 2.49)){
      " <img src='http://informedia.ws/projects/tuts/images/stars/2.png' width='90' height='20'>";
    }

    if((($tut_rating  >= 2.50)or($tut_rating == 2.50)) && ($tut_rating <= 2.99)){
     "<img src='http://informedia.ws/projects/tuts/images/stars/2point5.png' width='90' height='20'>";
    }

    if((($tut_rating  >= 3.00)or($tut_rating == 3.00)) && ($tut_rating <= 3.49)){
      " <img src='http://informedia.ws/projects/tuts/images/stars/3.png' width='90' height='20'>";
    }

    if((($tut_rating  >= 3.50)or($tut_rating == 3.50)) && ($tut_rating <= 3.99)){
       " <img src='http://informedia.ws/projects/tuts/images/stars/3point5.png' width='90' height='20'>";
    }
    if((($tut_rating  >= 4.00)or($tut_rating == 4.00)) && ($tut_rating <= 4.49)){
      "  <img src='http://informedia.ws/projects/tuts/images/stars/4.png' width='90' height='20'>";
    }
    if((($tut_rating  >= 4.50)or($tut_rating == 4.50)) && ($tut_rating <= 4.99)){
      " <img src='http://informedia.ws/projects/tuts/images/stars/4point5.png' width='90' height='20'>";
    }
    if($tut_rating  == 5.0){
      " <img src='http://informedia.ws/projects/tuts/images/stars/5.png' width='90' height='20'>";
    }
}

?... Now the stars don't appear at all.. [a href=\"http://informedia.ws/projects/tuts/viewcat.php?act=maincat&id=1\" target=\"_blank\"]http://informedia.ws/projects/tuts/viewcat...ct=maincat&id=1[/a]

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 20 June 2006 - 04:18 PM

No, I wanted you to replace the echo statement with a return statement.

I took a look at your code and cleaned it up a little. Take a look at my rewritten function:
<?php
function tut_stars($tut_rating){
        $sql = "SELECT * FROM `tutorials` WHERE `id` = '$id'";
        $result=mysql_query($sql);
        $rows=mysql_fetch_array($result);
        $rating = $rows['rating'];
        switch (TRUE) {
           case ($tut_rating >= 0 && $tut_rating < 0.5):
                $stars = '0';
                break;
           case ($tut_rating >= 0.5 && $tut_rating < 1):
                $stars = '0point5';
                break;
           case ($tut_rating >= 1 && $tut_rating < 1.5):
                $stars = '1';
                break;
           case ($tut_rating >= 1.5 && $tut_rating < 2):
                $stars = '1point5';
                break;
           case ($tut_rating >= 2 && $tut_rating < 2.5):
                $stars = '2';
                break;
           case ($tut_rating >= 2.5 && $tut_rating < 3):
                $stars = '2point5';
                break;
           case ($tut_rating >= 3 && $tut_rating < 3.5):
                $stars = '3';
                break;
           case ($tut_rating >= 3.5 && $tut_rating < 4):
                $stars = '3point5';
                break;
           case ($tut_rating >= 4 && $tut_rating < 4.5):
                $stars = '4';
                break;
           case ($tut_rating >= 4.5 && $tut_rating < 5):
                $stars = '4point5';
                break;
           case ($tut_rating >= 5):
                $stars = '5';
                break;
           default:
                $stars = '0';
        }
        return ("<img src='http://informedia.ws/projects/tuts/images/stars/" . $stars . ".png' width='90' height='20'>");
}?>

Ken

#7 lpxxfaintxx

lpxxfaintxx
  • Members
  • PipPipPip
  • Advanced Member
  • 181 posts

Posted 20 June 2006 - 04:34 PM

Ah, thank you so much. Works like a charm :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users