SitePoint Sponsor

User Tag List

Results 1 to 14 of 14

Thread: loop?

  1. #1
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is script:

    <?php

    $result = mysql_query("SELECT category.ID, category.Cat, subcategory.ID, subcategory.CID, " .
    "subcategory.SubCat, authors.ID, authors.Name FROM category, subcategory, authors");
    if (!$result) {
    echo("<P>Error retrieving from database!<BR>".
    "Error: " . mysql_error());
    exit();
    }

    $result = mysql_fetch_array($result);
    $listcatid = $result[category.ID];
    $listcat = $result["Cat"];
    $listsubid = $result[subcategory.ID];
    $listsubcid = $result["CID"];
    $listsubcat = $result["SubCat"];
    $listauthorid = $result[authors.ID];
    $listname = $result["Name"];
    ?>

    <table width="100%"><tr>
    <td><b>Category/b><br><? echo("$listcatid. $listcat <br>"); ?></td>
    <td><b>SubCategory/b><br><? echo("$listsubcid - $listsubid. $listsubcat <br>"); ?></td>
    <td><b>Authors/b><br><? echo("$listauthorid. $listname <br>"); ?></td>
    <br><br>

    Firstly it doesn't display: $listcatid, $listsubid, $listauthorid

    Secondly, the results in table only dispaly one result, is there a way, i can display every record?

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "Firstly it doesn't display: $listcatid, $listsubid, $listauthorid".. make sure you select all the fields you plan to use, else use *.

    I usually use the following code for loops and retrieving mysql data..
    (cannot use $result = mysq_fetch.. otherwise it will not work for more than one loop).

    if($myrow = mysql_fetch_array($result))
    do {

    echo ("results here");

    } while($myrow = mysql_fetch_array($result));

    else
    {
    echo "No records!";
    }

  3. #3
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    okay, now i have this:

    -------

    <?php
    $result = mysql_query("SELECT category.ID, category.Cat, subcategory.ID, subcategory.CID, " .
    "subcategory.SubCat, authors.ID, authors.Name FROM category, subcategory, authors");
    if (!$result) {
    echo("<P>Error retrieving from database!<BR>".
    "Error: " . mysql_error());
    exit();
    }
    ?>

    # there is form and table here
    <select name="authorid">
    <?

    while ($result = mysql_fetch_array($result)) {
    $listauthorid = $result[authors.ID];
    $listname = $result["Name"];
    echo("<option value='$listauthorid'>$listname</option> <br>");
    } ?></select>

    </b></td>
    <td width="114"><b>SubcategoryID: </b></td>
    <td width="150"> <b>
    <select name="subid">
    <?
    while ($result = mysql_fetch_array($result)) {
    $listsubid = $result[subcategory.ID];
    $listsubcat = $result["SubCat"];
    echo("<option value='$listsubid'>$listsubcat</option> <br>");
    } ?></select>
    </b></td>
    </tr>
    <tr>
    <td width="71"><b>Title: </b></td>
    <td width="151"> <b>
    <input type=TEXT name="title" size=25 maxlength=100>
    </b></td>
    <td width="114"><b>Category/b></td>
    <td width="150"> <b>
    <select name="catid">
    <?
    while ($result = mysql_fetch_array($result)) {
    $listcatid = $result[category.ID];
    $listcat = $result["Cat"];
    echo("<option value='$listcatid'>$listcat</option> <br>");
    } ?></select>

    --------

    and only the first select box displays data, if i remove that select box, the next one along the line is the only to display. How can i make al display.

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    !!$result = mysql_fetch_array($result))!!

    If you over write the value in $result, mysql is not provided with a valid array descriptor to fetch!

    pretend I had code with a command in..

    $command = "Peter, go and get that data and put the value in $command";

    so Peter did that..
    but next time, $command said something like "two oranges, three bananas", Peter wouldn't know what to do.

    so, you must do something like

    $somethingdifferenttoresult = mysql_fetch_array($result))

    I also notice that you are trying to use the $result over and over again for each set of list boxes. This is incorrect, you would need to create a copy of $result and then copy it in to $result each time you wanted to start from the first record again.
    <Edited by PeterW on 01-06-2001 at 11:00 AM>

  5. #5
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Got it...

    Thanx

    It was what you said
    <Edited by petesmc on 01-06-2001 at 11:02 AM>

  6. #6
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <edit>Okay </edit>

    I dont think you understand what I mean..
    <?

    while ($THISMUSTBEDIFFERENT = mysql_fetch_array($TOTHIS))
    {
    //...
    }
    ?>


  7. #7
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh my god....i h8 PHP

    Now, it doesn't add the results to database

    Only the results from all of the select boxes.....

    any idea?

  8. #8
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    We would probably need to see the code to tell what is wrong

  9. #9
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <!-- newarticle.php -->
    <HTML>
    <HEAD>
    <TITLE> Add New article </title><meta http-equiv="expires" content="0">
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache"> </head>

    <BODY>
    <?php
    include("articles.inc");
    mysql_select_db("petesmc_articles");
    if ($submit): // A new article has been entered
    // using the form below.

    $sql = "INSERT INTO articles SET " .
    "AuthorID='$aauthorid', " .
    "CatID='$ccatid', " .
    "SubID='$ssubid', " .
    "DateAdded='CURDATE()', " .
    "Description='$description', " .
    "Title='$title', " .
    "Status='$status', " .
    "Text='$text'";
    if (mysql_query($sql)) {
    echo("<P>New article added</P>");
    } else {
    echo("<P>Error adding new article: " .
    mysql_error() . "</P>");
    }

    ?>

    <P><A HREF="<?php echo($PHP_SELF); ?>">Add another article</A></P>
    <P><A HREF="articles.php">Return to article list</A></P>

    <?php
    else: // Allow the user to enter a new article

    $result = mysql_query("SELECT authors.ID, authors.Name FROM authors");

    $result2 = mysql_query("SELECT category.ID, category.Cat FROM category");

    $result3 = mysql_query("SELECT subcategory.ID, subcategory.SubCat FROM subcategory");

    ?>
    <FORM ACTION="<?php echo($PHP_SELF); ?>" METHOD=GET>
    </tr></table><br><br>
    <P><font size="6" color="#0066FF">Enter the new Article/font><BR>
    </P>


    <blockquote><table width="536" border="1" cellspacing="0" cellpadding="5" align="left">
    <tr>
    <td width="71"><b>AuthorID/b></td>
    <td width="151"> <b>
    <select name="authorid" size="1">
    <?
    while ($resulta = mysql_fetch_array($result)) {
    $listauthorid = $resulta[authors.ID];
    $listname = $resulta["Name"];
    echo("<option value=$listauthorid>$listname</option>");
    } ?></select>

    </b></td>
    <td width="114"><b>SubcategoryID: </b></td>
    <td width="150"> <b>
    <select name=subid>
    <?
    while ($result3a = mysql_fetch_array($result3)) {
    $listsubid = $result3a[subcategory.ID];
    $listsubcat = $result3a["SubCat"];
    echo("<option value=$listsubid>$listsubcat</option>");
    } ?></select>
    </b></td>
    </tr>
    <tr>
    <td width="71"><b>Title: </b></td>
    <td width="151"> <b>
    <input type=TEXT name="title" size=25 maxlength=100>
    </b></td>
    <td width="114"><b>Category/b></td>
    <td width="150"> <b>
    <select name=catid>
    <?
    while ($result2a = mysql_fetch_array($result2)) {
    $listcatid = $result2a[category.ID];
    $listcat = $result2a["Cat"];
    echo("<option value=$listcatid>$listcat</option>");
    } ?></select>
    </b></td>
    </tr>
    <tr>
    <td width="71"><b>Status/b></td>
    <td width="151"> <b>
    <select name="status">
    <option>Select Status type .....</option>
    <option value="active">Active</option>
    <option value="featured">Featured</option>
    <option value="hidden">Hidden</option>
    </select>
    </b></td>
    <td colspan="2"><b></b><b></b></td>
    </tr>
    <tr>
    <td colspan="4" height="26"><b> Description: </b></td>
    </tr>
    <tr>
    <td colspan="4"> <b>
    <textarea name="description" rows=6 cols=60 WRAP></textarea>
    </b></td>
    </tr>
    <tr>
    <td colspan="4"><b>Article Text/b></td>
    </tr>
    <tr>
    <td colspan="4"> <b>
    <textarea name="text" rows=25 cols=100 WRAP></textarea>
    </b></td>
    </tr>
    <tr>
    <td colspan="4">
    <input type=SUBMIT name="submit" value="SUBMIT ARTICLE">
    <input type="reset" name="Reset" value="RESET">
    </td>
    </tr>
    </table></blockquote>
    <P> <BR>
    </P>
    </FORM>



    <?php endif; ?>


    </BODY>
    </HTML>

  10. #10
    SitePoint Evangelist
    Join Date
    Jul 2000
    Location
    Warwickshire, England
    Posts
    557
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $sql = "INSERT INTO articles SET " .
    "AuthorID='$aauthorid', " .
    "CatID='$ccatid', " .
    "SubID='$ssubid', " .
    "DateAdded='CURDATE()', " .
    "Description='$description', " .
    "Title='$title', " .
    "Status='$status', " .
    "Text='$text'";

    By looking at your subsequent sql queries it seems you have multiple tables, so trying to insert data into fields that dont exist will not work.

  11. #11
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am only inserting to one table in this script.

    Could you try and create a simple script, where you insert using a select box?

    <Edited by petesmc on 01-06-2001 at 11:49 AM>

  12. #12
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i did it. For some reason you can't do tis:

    $listcatid = $result2a["category.ID"];

    I got rid of category, and made there different queries to table:


    $listcatid = $result2a["ID"];
    <Edited by petesmc on 01-06-2001 at 12:11 PM>

  13. #13
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmmm...

    Can you do this:

    <select name="authorid" size="1">
    <? $result = mysql_query("SELECT ID, Name FROM authors");
    while ($resulta = mysql_fetch_array($result)) {
    $listauthorid = $resulta["ID"];
    $listname = $resulta["Name"];
    echo("<option value=$listauthorid); ?><? if ($listauthorid==$authorid) {
    echo "selected"; } ?> <? echo("$listname</option>");
    } ?></select>


    authorid is specified above the above text. I am gettign a parse error where it say echo "selected";

    What i am trying to do is, pull data from table, put into select obx, then with an if statemnt, i am saying if the id is equal to the id in the other table ($authorid), then that is selected.

  14. #14
    <? echo "Kick me"; ?> petesmc's Avatar
    Join Date
    Nov 2000
    Location
    Hong Kong
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't worry, i downloaded the files from kevin Yanks tutorials:

    <SELECT NAME="authorid" SIZE=1>
    <?php
    $authors = mysql_query("SELECT ID, Name FROM authors");
    while ($author = mysql_fetch_array($authors)) {
    $listauthorid = $author["ID"];
    $listaname = $author["Name"];
    if ($authorid == $listauthorid) {
    echo("<OPTION SELECTED VALUE='$listauthorid'>$listaname\n");
    } else {
    echo("<OPTION VALUE='$listauthorid'>$listaname\n");
    }
    }
    ?>
    </SELECT>


    That is how you do it.


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
  •