SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Echo Statement Problem

    This code is in an include page that is included on almost every page of my site:

    <?php foreach ($data as $r) { print $r['NNJB']; } ?>

    As you can see, it refers to a cell in a database table named "NNJB." But some cells will have a default value, which I want to echo. For example, I might put the following on top of a page:

    $suggest = '<a href="/about/suggest/">Suggest a Nickname</a>';

    So I need to put an echo function in the corresponding cells in the database. But I can't seem to find the proper format. None of the following work:

    <?php echo "$suggest" ?>
    <?php echo '$suggest' ?>
    ' . $suggest . '
    $suggest

    Again, I'm looking for a value to insert in a database table, which will be printed out in place of NNJB in the following code:

    <?php foreach ($data as $r) { print $r['NNJB']; } ?>

    Thank you.

  2. #2
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $suggest '<a href="/about/suggest/">Suggest a Nickname</a>';
    echo 
    $suggest
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  3. #3
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    PHP Code:
    <?php foreach ($data as $r) { print $r['NNJB']; } ?>
    $r is already the value

    take a look at the foreach syntax in the php manual


    PHP Code:
    $sql mysql_query("SELECT field FROM NNJB");
     
    while (
    $r mysql_fetch_array($sql))
    {
        print 
    $r['field'] .'<br>';

    would be something you may try
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  4. #4
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by frezno
    $r is already the value

    take a look at the foreach syntax in the php manual


    PHP Code:
    $sql mysql_query("SELECT field FROM NNJB");
     
    while (
    $r mysql_fetch_array($sql))
    {
        print 
    $r['field'] .'<br>';

    would be something you may try
    OK, I understand - thanks.

    I tried your code, but it gives me this error message:


    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\sites\mysite\includes\one.php on line 10

  5. #5
    does not play well with others frezno's Avatar
    Join Date
    Jan 2003
    Location
    Munich, Germany
    Posts
    1,391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should use your table and fieldnames and i assume there's an existing connection to the database...
    We are the Borg. Resistance is futile. Prepare to be assimilated.
    I'm Pentium of Borg.Division is futile.Prepare to be approximated.

  6. #6
    <? james('rules'); ?>
    Join Date
    Jun 2004
    Location
    Wales, UK
    Posts
    789
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe, for someone with 463 posts that was a simple error .


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
  •