SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problems with Undefined index

    Please do somebody help me with this: each time that I run the program, I receive those erros:

    Notice: Undefined index: startLimit in D:\Program Files\Apache Group\Apache2\htdocs\DoJoSolutions\Books_Lister.php on line 36

    Notice: Undefined index: rows in D:\Program Files\Apache Group\Apache2\htdocs\DoJoSolutions\Books_Lister.php on line 37

    Notice: Undefined index: sortBy in D:\Program Files\Apache Group\Apache2\htdocs\DoJoSolutions\Books_Lister.php on line 38

    Notice: Undefined index: sortOrder in D:\Program Files\Apache Group\Apache2\htdocs\DoJoSolutions\Books_Lister.php on line 39

    Notice: Undefined variable: orderByQuery in D:\Program Files\Apache Group\Apache2\htdocs\DoJoSolutions\Books_Lister.php on line 66


    Notice: Undefined index: startLimit in D:\Program Files\Apache Group\Apache2\htdocs\DoJoSolutions\Books_Lister.php on line 88

    This are part of my codes:

    <?
    $initStartLimit = 0;
    $limitPerPage = 10;



    $startLimit = $_REQUEST['startLimit'];
    $numberOfRows = $_REQUEST['rows'];
    $sortBy = $_REQUEST['sortBy'];
    $sortOrder = $_REQUEST['sortOrder'];

    if ($startLimit=="")
    {
    $startLimit = $initStartLimit;
    }

    if ($numberOfRows=="")
    {
    $numberOfRows = $limitPerPage;
    }

    if ($sortOrder=="")
    {
    $sortOrder = "DESC";
    }
    if ($sortOrder == "DESC") { $newSortOrder = "ASC"; } else { $newSortOrder = "DESC"; }
    $limitQuery = " LIMIT ".$startLimit.",".$numberOfRows;
    $nextStartLimit = $startLimit + $limitPerPage;
    $previousStartLimit = $startLimit - $limitPerPage;

    if ($sortBy!="")
    {
    $orderByQuery = " ORDER BY ".$sortBy." ".$sortOrder;
    }


    $sql = "SELECT * FROM libo_books".$orderByQuery.$limitQuery;
    $result = MYSQL_QUERY($sql);
    $numberOfRows = MYSQL_NUM_ROWS($result);


    ?>
    <?
    if ($numberOfRows==0) {
    ?>

    Sorry. No records found !!

    <?
    }
    else if ($numberOfRows>0) {

    $i=0;
    ?>


    <br>
    <?
    if ($_REQUEST['startLimit'] != "")
    {
    ?>

    <a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $previousStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Previous <? echo $limitPerPage; ?> Results</a>....
    <? } ?>
    <?
    if ($numberOfRows == $limitPerPage)
    {
    ?>
    <a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $nextStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Next <? echo $limitPerPage; ?> Results</a>
    <? } ?>

    Please, Do somebody give an Idea how to get over this erros.

    Thanks.

  2. #2
    SitePoint Addict liquidautumn's Avatar
    Join Date
    Nov 2002
    Location
    Kharkov, Ukraine
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    it's notices, appearing when you are refering to array element, which not exists

    you may safely supress these, i believe unimportant, notices with "@" in SUCH cases

  3. #3
    SitePoint Wizard
    Join Date
    Oct 2001
    Posts
    2,686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Instead of supressing the error I would try to fix it. Here is a revised version of your script. I've used the ternary operator (expr1) ? (expr2) : (expr3). As you see the script was reduced over 1/3 in number of lines.

    PHP Code:
    <?
    $initStartLimit 
    0;
    $limitPerPage 10;

    $startLimit   = isset($_REQUEST['startLimit']) ? $_REQUEST['startLimit'] : $initStartLimit;
    $numberOfRows = isset($_REQUEST['rows'])       ? $_REQUEST['rows']       : $limitPerPage;
    $sortOrder    = isset($_REQUEST['sortOrder'])  ? $_REQUEST['sortOrder']  : 'DESC';
    $sortBy       = isset($_REQUEST['sortBy'])     ? $_REQUEST['sortBy']     : '';

    $newSortOrder $sortOrder == 'DESC' 'ASC' 'DESC';

    $limitQuery " LIMIT ".$startLimit.",".$numberOfRows;
    $nextStartLimit $startLimit $limitPerPage;
    $previousStartLimit $startLimit $limitPerPage;

    if (
    $sortBy != "") {
        
    $orderByQuery " ORDER BY ".$sortBy." ".$sortOrder;
    }


    $sql "SELECT * FROM libo_books" $orderByQuery $limitQuery;
    $result MYSQL_QUERY($sql);
    $numberOfRows MYSQL_NUM_ROWS($result);


    if (
    $numberOfRows == 0) { 
        echo 
    'Sorry. No records found !!';
    } elseif (
    $numberOfRows 0) {
        
    $i=0;
    }


    if (
    $_REQUEST['startLimit'] != "") {
        echo 
    "<a href=\"" $_SERVER['PHP_SELF'] , 
             
    "?startLimit=$previousStartLimit,
             
    "&amp;limitPerPage=$limitPerPage,
             
    "&amp;sortBy=$sortBy,
             
    "&amp;sortOrder=$sortOrder\">" ,
             
    "Previous $limitPerPage Results</a>....";
    }

    if(
    $numberOfRows == $limitPerPage) {
        echo 
    "<a href=\"" $_SERVER['PHP_SELF'] , 
             
    "?startLimit=$nextStartLimit,
             
    "&amp;limitPerPage=$limitPerPage,
             
    "&amp;sortBy=$sortBy,
             
    "&amp;sortOrder=$sortOrder\">" ,
             
    "Next $limitPerPage Results</a>....";
    }
    -Helge

  4. #4
    SitePoint Addict liquidautumn's Avatar
    Join Date
    Nov 2002
    Location
    Kharkov, Ukraine
    Posts
    210
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i have to read over the script, not only first lines

    elegant solution, Helge

    however, in most cases, where such solition not applicable, doing $var = isset($arr['index']) ? $arr['index'] : ''; have no preference over supressing notice

  5. #5
    SitePoint Member
    Join Date
    Apr 2005
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this is the hold code I still receive the same error:
    check, Please what I did wrong.

    <?
    $initStartLimit = 0;
    $limitPerPage = 10;



    $startLimit = $_REQUEST['startLimit'];
    $numberOfRows = $_REQUEST['rows'];
    $sortBy = $_REQUEST['sortBy'];
    $sortOrder = $_REQUEST['sortOrder'];

    if ($startLimit=="")
    {
    $startLimit = $initStartLimit;
    }

    if ($numberOfRows=="")
    {
    $numberOfRows = $limitPerPage;
    }

    if ($sortOrder=="")
    {
    $sortOrder = "DESC";
    }
    if ($sortOrder == "DESC") { $newSortOrder = "ASC"; } else { $newSortOrder = "DESC"; }
    $limitQuery = " LIMIT ".$startLimit.",".$numberOfRows;
    $nextStartLimit = $startLimit + $limitPerPage;
    $previousStartLimit = $startLimit - $limitPerPage;

    if ($sortBy!="")
    {
    $orderByQuery = " ORDER BY ".$sortBy." ".$sortOrder;
    }


    $sql = "SELECT * FROM libo_books".$orderByQuery.$limitQuery;
    $result = MYSQL_QUERY($sql);
    $numberOfRows = MYSQL_NUM_ROWS($result);


    ?>
    <?
    if ($numberOfRows==0) {
    ?>

    Sorry. No records found !!

    <?
    }
    else if ($numberOfRows>0) {

    $i=0;
    ?>


    <br>
    <?
    if ($_REQUEST['startLimit'] != "")
    {
    ?>

    <a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $previousStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Previous <? echo $limitPerPage; ?> Results</a>....
    <? } ?>
    <?
    if ($numberOfRows == $limitPerPage)
    {
    ?>
    <a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $nextStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Next <? echo $limitPerPage; ?> Results</a>
    <? } ?>

    <br><br>
    <TABLE CELLSPACING="0" CELLPADDING="3" BORDER="0" WIDTH="80%">
    <TR>
    <TD>
    <a href="<? echo $PHP_SELF; ?>?sortBy=Book_Id&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
    <B>Book_Id</B>
    </a>
    </TD>
    <TD>
    <a href="<? echo $PHP_SELF; ?>?sortBy=Title&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
    <B>Title</B>
    </a>
    </TD>
    <TD>
    <a href="<? echo $PHP_SELF; ?>?sortBy=Author&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
    <B>Author</B>
    </a>
    </TD>
    <TD>
    <a href="<? echo $PHP_SELF; ?>?sortBy=Ilustrator&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
    <B>Ilustrator</B>
    </a>
    </TD>
    <TD>
    <a href="<? echo $PHP_SELF; ?>?sortBy=Description&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
    <B>Description</B>
    </a>
    </TD>
    <TD>
    <a href="<? echo $PHP_SELF; ?>?sortBy=ParOfSerie&sortOrder=<? echo $newSortOrder; ?>&startLimit=<? echo $startLimit; ?>&rows=<? echo $limitPerPage; ?>">
    <B>ParOfSerie</B>
    </a>
    </TD>
    </TR>
    <?
    while ($i<$numberOfRows)
    {

    if (($i%2)==0) { $bgColor = "#FFFFCC"; } else { $bgColor = "#C0C0C0"; }

    $thisBook_Id = MYSQL_RESULT($result,$i,"Book_Id");
    $thisTitle = MYSQL_RESULT($result,$i,"Title");
    $thisAuthor = MYSQL_RESULT($result,$i,"Author");
    $thisIlustrator = MYSQL_RESULT($result,$i,"Ilustrator");
    $thisDescription = MYSQL_RESULT($result,$i,"Description");
    $thisParOfSerie = MYSQL_RESULT($result,$i,"ParOfSerie");

    ?>
    <TR BGCOLOR="<? echo $bgColor; ?>">
    <TD><? echo $thisBook_Id; ?></TD>
    <TD><? echo $thisTitle; ?></TD>
    <TD><? echo $thisAuthor; ?></TD>
    <TD><? echo $thisIlustrator; ?></TD>
    <TD><? echo $thisDescription; ?></TD>
    <TD><? echo $thisParOfSerie; ?></TD>
    <TD><a href="editLibo_books.php?Book_IdField=<? echo $thisBook_Id; ?>">Edit</a></TD>
    <TD><a href="confirmDeleteLibo_books.php?Book_IdField=<? echo $thisBook_Id; ?>">Delete</a></TD>
    </TR>
    <?
    $i++;

    } // end while loop
    ?>
    </TABLE>


    <br>
    <?
    if ($_REQUEST['startLimit'] != "")
    {
    ?>

    <a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $previousStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Previous <? echo $limitPerPage; ?> Results</a>....
    <? } ?>
    <?
    if ($numberOfRows == $limitPerPage)
    {
    ?>
    <a href="<? echo $_SERVER['PHP_SELF']; ?>?startLimit=<? echo $nextStartLimit; ?>&limitPerPage=<? echo $limitPerPage; ?>&sortBy=<? echo $sortBy; ?>&sortOrder=<? echo $sortOrder; ?>">Next <? echo $limitPerPage; ?> Results</a>
    <? } ?>

    <br><br>
    <?
    } // end of if numberOfRows > 0
    ?>
    </div>
    </body>
    </html>


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
  •