SitePoint Sponsor

User Tag List

Results 1 to 13 of 13

Thread: Need help with my function, im absolutly lost :(

  1. #1
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with my function, im absolutly lost :(

    Hi im trying to make a function that i can display in one of my forms, but im having much difficulty getting it right, im pretty new at php so ive kinda hit a brick wall.

    I know the problems im having are with the following line

    PHP Code:
    <option value="$c_row['id']"$selected>$c_row['name']</option
    Also im not sure if there are other problems

    Can anyone help me clean this up a little?

    Thanks in advance for any guidance.

    PHP Code:
    function cats () {
    $c_query mysql_query("SELECT * FROM cats");

        while ( 
    $c_row mysql_fetch_assoc($c_query) )

                            {

        
    $selected = ($sub_action[0] == $c_row['id']) ? " SELECTED" "";

    <
    option value="$c_row['id']"$selected>$c_row['name']</option>

                            }

        
    $c_query mysql_query("SELECT * FROM cats WHERE `id`='{$sub_action[0]}'");

        
    $c_row   mysql_fetch_assoc($c_query);
                            
                            } 

  2. #2
    SitePoint Addict dbr's Avatar
    Join Date
    Aug 2006
    Location
    Tucked away in the mountains...
    Posts
    228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo "<option value="$c_row['id']"$selected>$c_row['name']</option> ";
    "Three components make an entrepreneur:
    the person, the idea, and the resources to make it happen."
    Anita Roddick ~British entrepreneur
    dbr founder of: ProximityCast.com

  3. #3
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply, ive tried that already and get


    Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in /home/me/public_html/file.php on line

  4. #4
    SitePoint Zealot
    Join Date
    Mar 2006
    Posts
    139
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think the above line needs to be modified into the following form:

    PHP Code:
     echo "<option value=\"$c_row['id']\" $selected>$c_row['name']</option>"

  5. #5
    SitePoint Evangelist priti's Avatar
    Join Date
    Aug 2006
    Location
    India
    Posts
    488
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Para View Post
    Hi im trying to make a function that i can display in one of my forms, but im having much difficulty getting it right, im pretty new at php so ive kinda hit a brick wall.

    I know the problems im having are with the following line

    PHP Code:
    <option value="$c_row['id']"$selected>$c_row['name']</option
    Also im not sure if there are other problems

    Can anyone help me clean this up a little?

    Thanks in advance for any guidance.

    PHP Code:
    function cats () {
    $c_query mysql_query("SELECT * FROM cats");

        while ( 
    $c_row mysql_fetch_assoc($c_query) )

                            {

        
    $selected = ($sub_action[0] == $c_row['id']) ? " SELECTED" "";

    <
    option value="$c_row['id']"$selected>$c_row['name']</option>

                            }

        
    $c_query mysql_query("SELECT * FROM cats WHERE `id`='{$sub_action[0]}'");

        
    $c_row   mysql_fetch_assoc($c_query);
                            
                            } 
    <option value="$c_row['id']"$selected>$c_row['name']</option>

    hmm track your values first and in your code try this

    echo "<option value="$c_row['id']" $selected>$c_row['name']</option>"
    i see u forgot to give space between $selected and value so it might be prompting you to provide "." as php is seeing "$c_row['id']"$selected" together and missing "." fails to create value for your option.

    and what is $sub_action[0]???? where have u defined it try to find echo its value and find are you getting the value u are expecting??

    hope something will be helpful for u .

  6. #6
    SitePoint Member
    Join Date
    Dec 2006
    Location
    Germany, but living in Denmark
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <option value="$c_row['id']".$selected->$c_row['name']</option>

  7. #7
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    505
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echo '<option value="'.$c_row[id].'"'. $selected . '>' . $c_row[name] . '</option>';

    Try this
    Barbara

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Morning Para.

    Couple of things to think about....

    If you are using a variable in a function such as $sub_action[0] it needs to be within the scope of the function itself and as such needs to be passed as an argument to it.

    PHP Code:
    function cats($variable) {


    Amended your code to:
    PHP Code:
    function cats ($sub_action) { 
        
    $c_query mysql_query("SELECT * FROM cats"); 
        while ( 
    $c_row mysql_fetch_assoc($c_query) ) { 
            
            
    $selected = ($sub_action == $c_row['id']) ? " SELECTED" ""
            
            echo 
    '<option value="'$c_row['id'] .'" '$selected .'>'$c_row['name'].'</option>'
                            } 

        
    $c_query mysql_query("SELECT * FROM cats WHERE `id`='"$sub_action ."'"); 
        
    $c_row   mysql_fetch_assoc($c_query); 
                 

    usage
    PHP Code:
    cats($sub_action[0]); 
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  9. #9
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    spikeZ, your a bloody genius mate, your advice is always precise!

    Works a treat my friend! Thanks for the help my brother.


  10. #10
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Thank you very much
    Pleasure to help.
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

  11. #11
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Back again i spoke to soon lol

    Let me explain the probelms im having.

    The reason i was trying to write this function is because i had problems displaying some code within my if statement.

    PHP Code:
    if(isset($_COOKIE["username"])){
    echo 
    'I WANT TO DISPLAY MY CODE HERE';


    }
    else {
    echo 
    'You must be logged in to do this!';


    The code i want to display is below, but the php inside the code is causing errors, thats why i thought a function for the php would be the best route, but at the moment the function is not displaying.

    This is the code before i wrote function.

    If anyone could help with this id much apprciate it.


    PHP Code:
    <form method="post" action="index.php?app=layouts&action=thankyou" enctype="multipart/form-data">

    <table width="100%" cellspacing="0" cellpadding="5">



        <tr>

            <td class="content_text">

                <table cellpadding="0" cellspacing="0" border="0" width="100%">
    <tr>

                        <td class="text">Layout Title</td>

                        <td class="info"><input name="layout_title" type="text" value="" size="45" /><br /><br /></td>
                    </tr>

                    <tr>

                        <td class="text">Category</td>

                        <td class="info">

                            <select name="cat" style="width: 250px">

            
                            <?php

                            $c_query 
    mysql_query("SELECT * FROM cats");

                            while ( 
    $c_row mysql_fetch_assoc($c_query) )

                            {

                                
    $selected = ($sub_action[0] == $c_row['id']) ? " SELECTED" "";

                            
    ?>

                                <option value="<?=$c_row['id']?>"<?=$selected?>><?=$c_row['name']?></option>

                            <?php

                            
    }

                            
    $c_query mysql_query("SELECT * FROM cats WHERE `id`='{$sub_action[0]}'");

                            
    $c_row   mysql_fetch_assoc($c_query);



                            
    ?>

                    
                            </select>                    <br /><br /></td>
                    </tr>

                    <tr>

                        <td class="text">Thumbnail:</td>

                        <td class="info"><input type="file" name="thumbnail" size="45" maxlength="255" /><br /><br /></td>
                    </tr>
    <tr>

                        <td class="text">Code 1 Title</td>

                        <td class="info"><input name="layout_code1title" type="text" value="Example: Copy and paste to About me." size="45" /><br /><br /></td>
                    </tr>
                    <tr>

                        <td class="text">Code 1 </td>

                        <td class="info"><textarea name="layout_code1" cols="45" rows="10"></textarea>
                        <br /><br /></td>
                    </tr>
    <tr>

                        <td class="text">Code 2 Title (Optional) </td>

                        <td class="info"><input name="layout_code2title" type="text" value="" size="45" /><br /><br /></td>
                    </tr>
                    <tr>

                        <td class="text">Code 2 (Optional) </td>

                        <td class="info"><textarea name="layout_code2" cols="45" rows="10"></textarea><br /><br /></td>
                    </tr>
                    <tr>

                        <td class="text">Code 3 Title (Optional) </td>

                        <td class="info"><input name="layout_code3title" type="text" value="" size="45" /><br /><br /></td>
                    </tr>
                    <tr>

                        <td class="text">Code 3 (Optional) </td>

                        <td class="info"><textarea name="layout_code3" cols="45" rows="10" ></textarea><br /><br /></td>
                    </tr>
                                <tr>

                        <td class="text">Layout Notes (Optional) </td>

                        <td class="info"><textarea name="layout_desc" cols="45" rows="5"></textarea><br /><br /></td>
                    </tr>
                       <tr>

                        <td class="text">Layout Tags </td>

                        <td class="info"><input name="layout_tags" type="text" value="Example: girly,myspace,pink," size="45" /><br /><br /></td>
                    </tr>
                </table>

            </td>

        </tr>

        <tr>

            <td class="save"><input id="button" type="submit" value="Submit Layout" class="button" /></td>

        </tr>

    </table>

    </form>

  12. #12
    SitePoint Zealot Para's Avatar
    Join Date
    Mar 2005
    Location
    London UK
    Posts
    155
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    EDIT: fixed, i needed to close '; the html before the function

  13. #13
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,538
    Mentioned
    79 Post(s)
    Tagged
    3 Thread(s)
    Just on a side note, try and get into the habit of using full code tags rather than short tags.
    what I mean is:
    <?php echo
    rather than
    <?=

    On some servers, short tags aren't supported so if you were to ever move your app elsewhere there is no guarantee it will work as expected
    Mike Swiffin - Community Team Leader

    Only a woman can read between the lines of a one word answer.....
    I started out with nothing... and still got most of it left!

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
  •