Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Question Drop Down Menu - Displaying PHP Elements

    Hi All,

    I currently have a Content Management System that allows the user to edit content they have already added to the system, it is displayed on this page with the following code:

    PHP Code:
    Edit An Existing Article:
    <br/>
    <?php
    $connection 
    mysql_connect("localhost""username""password") or die(mysql_error());
    mysql_select_db('dbname, $connection) or die(mysql_error());
    $query3 = mysql_query('
    SELECT COUNT(*) AS nb_messages FROM news');
    $data = mysql_fetch_array($query3);
    $totalDesMessages = $data['
    nb_messages];

    $query2 mysql_query("SELECT * FROM news");
    while (
    $donnees mysql_fetch_array($query2))
    {

    ?>
    <a href="form_editnews.php?id=<?php echo $donnees[0?>">|<?php echo $donnees[1?>|</a>
    <?php 
    }
    ?>
    <div id="newshelp"><a href="newshelp.html" target="_blank">?</a></div>
    </div>
    Although this works perfectly fine, in terms of it allows the user to edit the information they have submitted. If the user adds several articles the page starts to become cluttered.

    I was just wondering if it is possible to edit this articles using a drop down menu. Such as the drop down menu would contain the names of the articles that had been currently submitted and then when a user selects one from the menu, they are taken to a page where they can edit the information.

    Is this possible???

    Thanks in Advance

  • #2
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Yes, that is possible.

  • #3
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts
    Could you point me in the direction of a good tutorial or give me some advice, I tried something along the lines of the following but it returns blank field and doesnt link to anything:

    Code:
    <select name="selectnewsedit">
    PHP Code:
    <?php
    $connection 
    mysql_connect("localhost""username""password") or die(mysql_error());
    mysql_select_db('dbcontent'$connection) or die(mysql_error());
    $query3 mysql_query('SELECT COUNT(*) AS nb_messages FROM news');
    $data mysql_fetch_array($query3);
    $totalDesMessages $data['nb_messages'];

    $query2 mysql_query("SELECT * FROM news");
    while (
    $donnees mysql_fetch_array($query2))
    {
    ?>     
    <?php
    }
    ?>
    <option value="newsarticle1"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?> </a></option>
    <option value="newsarticle2"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?> </a><option>
    <option value="newsarticle3"><a href="form_editnews.php?id=<?php echo $donnees[0] ?>"><?php echo $donnees[1] ?>
    </a></option>

    </select>

  • #4
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    PHP Code:
    while ($donnees = mysql_fetch_array($query2))
    {
    ?>     
    <?php
    }
    ?>
    That would obviously not print out anything.

    However, judging from your post it looks like you didn't paste the code properly. Just take the entire section, and put it between one set of PHP tags. Don't try to seperate the HTML from the PHP.

  • #5
    Regular Coder
    Join Date
    Sep 2007
    Posts
    809
    Thanks
    5
    Thanked 2 Times in 2 Posts

    Question

    The Code in Full Is:

    PHP Code:
    Edit An Existing Article:
    <br/>

    <select name="selectnewsedit">
    <?php
    include("dbconnection.php");
    $query3 mysql_query('SELECT COUNT(*) AS nb_messages FROM news');
    $data mysql_fetch_array($query3);
    $totalDesMessages $data['nb_messages'];

    $query2 mysql_query("SELECT * FROM news");
    while (
    $donnees mysql_fetch_array($query2))
    {
    ?>     
    <?php
    }
    ?>
        <option value="newsarticle1"><a href="form_editnews.php?id=<?php echo $donnees[0?>"><?php echo $donnees[1?>    </a></option>
        <option value="newsarticle2"><a href="form_editnews.php?id=<?php echo $donnees[0?>"><?php echo $donnees[1?>    </a></option>
        <option value="newsarticle3"><a href="form_editnews.php?id=<?php echo $donnees[0?>"><?php echo $donnees[1?>    </a></option>
    </select>

    <div id="newshelp"><a href="newshelp.html" target="_blank">?</a></div>
    </div>
    Any ideas why this isnt showing the php articles in the drop down box???

    Cheers

  • #6
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    they are outside the while loop

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,961
    Thanks
    2
    Thanked 305 Times in 297 Posts
    Sorry to be extra blunt, but does anyone read their code after they write it anymore? Writing program logic that does what you want it to do, is after all what programming means.

    Aedrin pointed out that the while loop was empty and Matak pointed out that the lines that output the results are outside of the loop.

    The first step in troubleshooting code is to read it to make sure it is doing what you intend. The first step in troubleshooting is not to post your code on a forum and ask some other programmer to proof read it for you. Programming help forums are for getting help with programming questions, errors, and problems. Proof reading code to make sure it is what the programmer was thinking of doing, is not what programming help forums are for. Proof reading code is not a programming question, error, or problem, it is something that the programmer who wrote the code is supposed to be doing.
    Last edited by CFMaBiSmAd; 11-02-2007 at 02:36 PM. Reason: added even more bluntness
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Banned
    Join Date
    Apr 2007
    Posts
    428
    Thanks
    29
    Thanked 5 Times in 5 Posts
    OK, i'm troubled now couse i presumed that he can't access $donnesse variable (in this case array) outside the while loop.
    Why do i think that, as there is 50% chance that i'm wrong (and i guessed i didn't test it)

    #1 Case
    In this case you can access array outside while loop, as it's defined outside the while loop

    PHP Code:
    <?php

    $i
    1;
    $a = array();
    while (
    $i <10 ) {
        
        
    $a[] = $i;
        
    $i++;
    }

    print_r ($a);

    ?>
    #2 Case
    You cant access array outside while loop as it is defined in while loop

    PHP Code:
    <?php

    $i
    1;

    while (
    $i <10 ) {
        
    $a = array();
        
    $a[] = $i;
        
    $i++;
    }

    print_r ($a);

    ?>
    What i mean, is that you have array, but it's only the last number looped.

    So, this code

    while ($array = mysql_fetch_array($query)) {

    }


    behaves like #2
    Last edited by matak; 11-02-2007 at 03:18 PM. Reason: haha! i tested it :)

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You are correct.

    PHP's variable scope is on the function/method level, as opposed to for example Java's brace level.

    So the code posted by the user will work without errors, because the $donnees variable flows down, to outside of the while loop. This is why there is no warning.

    I won't ask the user why there are 3 <options> instead of just one.


  •  

    Posting Permissions

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