Jump to content

PHP & Functions


lpxxfaintxx

Recommended Posts

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:

[code]

$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>";

}
}

[/code]

Help would be awesome, thanks.
Link to comment
Share on other sites

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

<[color=blue]div class[/color]='[color=orange]tutorialtitle[/color]'> <[color=blue]span class[/color]='[color=orange]style1[/color]'><[color=blue]a href[/color]='[color=orange]viewtut.php?id=11[/color]'>Mike rocks<[color=blue]/a[/color]><[color=blue]/span[/color]><[color=blue]br /[/color]>
Author: <[color=blue]a href[/color]='[color=orange]#[/color]'>Mike<[color=blue]/a[/color]> <[color=blue]br[/color]> Rating: <[color=blue]span class[/color]='[color=orange]style3[/color]'><[color=blue]/span[/color]><[color=blue]/div[/color]>
<[color=blue]img src[/color]='[color=orange]tutorialchimp.com/images/logo[/color]' width='[color=orange]44[/color]' height='[color=orange]43[/color]' alt='[color=orange][/span]' /><[span style=\'color:blue\']br /[/color]>wwwwipiiee<[color=blue]br /[/color]>
<[color=blue]span class[/color]='[color=orange]style2[/color]'>22nd March 2006 <[color=blue]span class[/color]='[color=orange]style3[/color]'><[color=blue]a href[/color]='[color=orange]#[/color]'>Report Tutorial<[color=blue]/a[/color]><[color=blue]/span[/color]> <[color=blue]span class[/color]='[color=orange]style3[/color]'>Total Views: <[color=blue]strong[/color]>0<[color=blue]/strong[/color]><[color=blue]/span[/color]> <[color=blue]span class[/color]='[color=orange]style3[/color]'>Categorgy: <[color=blue]a href[/color]='[color=orange]viewcat.php?act=maincat&id=1[/color]'><[color=blue]strong[/color]>Adobe Photoshop<[color=blue]/strong[/color]><[color=blue]/a[/color]><[color=blue]/span[/color]><[color=blue]/span[/color]><[color=blue]/div[/color]><[color=blue]img src[/color]='[color=orange]http://informedia.ws/projects/tuts/images/stars/5.png[/color]' width='[color=orange]90[/color]' height='[color=orange]20[/color]'>[!--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

Link to comment
Share on other sites

Yes, I am 100% sure.

This is the function part:

[code]

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'>";
    }
}

[/code]
Link to comment
Share on other sites

So you want me to make the functions like this:

[code]

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'>";
    }
} [/code]

?... 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]
Link to comment
Share on other sites

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:
[code]<?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'>");
}?>[/code]

Ken
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.