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)

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

    How can I put a database variable like this...

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

    into an echo function, like this?

    PHP Code:
    <?php
    $myname 
    'Russia';
    ?>
    I've tried several variations similar to this...

    PHP Code:
    <?php
    $myname 
    '' . foreach ($data as $r){ print $r['NNGW']; } . '';
    ?>

  2. #2
    SitePoint Enthusiast jpp's Avatar
    Join Date
    Nov 2003
    Location
    Arnhem, The Netherlands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $myname 
    '';
    foreach (
    $data as $r){ $myname .= $r['NNGW']; };
    ?>
    Greetz, Jpp

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you sure? I replaced this...

    [PHP]
    <?php
    $mycode = 'deu';
    $myname = 'Germany';
    ?>
    [PHP]

    with this:

    PHP Code:
    <?php
    $mycode 
    'deu';
    foreach (
    $data as $r){ $myname .= $r['NNGW']; };
    ?>
    and I get this error message:

    Invalid argument supplied for foreach()

    Or am I doing something wrong? Thanks.

  4. #4
    SitePoint Enthusiast jpp's Avatar
    Join Date
    Nov 2003
    Location
    Arnhem, The Netherlands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by geosite
    Are you sure? I replaced this...

    [PHP]
    <?php
    $mycode = 'deu';
    $myname = 'Germany';
    ?>
    [PHP]

    with this:

    PHP Code:
    <?php
    $mycode 
    'deu';
    foreach (
    $data as $r){ $myname .= $r['NNGW']; };
    ?>
    and I get this error message:

    Invalid argument supplied for foreach()

    Or am I doing something wrong? Thanks.
    Does the array $data exist? Maybe you could post some more code and explain what you are trying to do. ok?

    Greetz, Jpp

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jpp
    Does the array $data exist? Maybe you could post some more code and explain what you are trying to do. ok?

    Greetz, Jpp
    Yes, I'm playing with a lot of functions that I don't really understand, and I've probably botched my code.

    Here's what I'm trying to do: I'm building a website with a page for each of the world's continents, nations, states, provinces, etc. I'm connected to a database and am able to display values for things like Name, Nickname, Capital, Population, etc.

    But here's where I get stuck...

    Every page has a series of echo functions like this:

    PHP Code:
    <?php
    $mycode 
    'deu';
    $myname 'Germany';
    $mykon 'Eurasia';
    $kon 'eur';
    $mynat 'Germany';
    $nat 'deu';
    $mystate '';
    $state '';
    $type 'nat'// Chooses Article Include (further defined by $mycode)
    ?>
    I'm actually going to have EIGHT websites patterned after this, with nearly 3,000 pages. So I don't want to have to open up every page and fiddle with the echo functions.

    So imagine if every page had just ONE echo function placed on it directly, though it displayed ALL the echo functions via an include. For example, France's page would look like this...

    PHP Code:
    <?php
    $mycode 
    'fra';
    ?>
    And all the other echo functions would be on an include page, like this:

    -----------------------------------
    INCLUDE PAGE

    PHP Code:
    <?php
    $myname 
    'France';
    $mykon 'Eurasia';
    $kon 'eur';
    $mynat 'France';
    $nat 'fra';
    $mystate '';
    $state '';
    $type 'nat'// Chooses Article Include (further defined by $mycode)
    ?>
    ----------------------------------------

    But since this include will be on EVERY page, $myname obviously can't be "France." So I need to Replace $myname = 'France'; with a function that taps into my database.

    Although I'm probably not ready to tackle it yet, let me give you a little broader picture. My database is organized on the "parent" model, with the U.S. listed as the parent of Alaska and North America the parent of the U.S.

    So let's say we're dealing with a continent page, like North America. The only values that will be DISPLAYED are North America and na, because North America has no parents.

    PHP Code:
    <?php
    $myname 
    '[DATABASE LINK]';
    $mykon '[DATABASE LINK]';
    $kon '[DATABASE LINK]';
    $mynat '[DATABASE LINK]';
    $nat '[DATABASE LINK]';
    $mystate '[DATABASE LINK]';
    $state '[DATABASE LINK]';
    $type 'nat'// Chooses Article Include (further defined by $mycode)
    ?>
    If the page is ALASKA ($mycode = 'us-ak', then "Alaska" and "ak" will be displayed, along with its parents, "United States" and "na" AND the United States' parents', "North America" and "na".

    But one of the most basic problems I'm dealing with is simply plugging a database variable into an echo function. You've apparently shown me the key, but I presumably have too many errors on my page.

    I'm in the middle of several experiments, but I posted the source code from y most succesful page so far below. It is a bit of a mess. For example, I have about three separate queries; I simply haven't yet figured out how to merge them into one or subtract any of them without screwing anything up.

    PHP Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <?php
    $mycode 
    'deu';
    $myname '';
    $mykon 'Eurasia';
    $kon 'eur';
    $mynat 'Germany';
    $nat 'deu';
    $type 'nat'// Chooses Article Include (further defined by $mycode)
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    <?php
    [DATABASE CONNECTION]

    // This first query may be useless.
    $query "
    SELECT * // Name, NNGW, QGW, SigGW, RegCSS
    FROM basics
    WHERE IDArea LIKE '
    $mycode'";
    $data = array();
    if (
    $res mysql_query($query)) { while ($temp mysql_fetch_assoc($res)) { $data[] = $temp;} }

    $query " SELECT Name, NNGW, QGW, SigGW
    FROM basics
    WHERE IDArea LIKE '
    $mycode'";
    $data = array();
    $res mysql_query($query);
    if (
    mysql_errno()){ echo "MySQL error ".mysql_errno().": ".mysql_error()."\n<br />When executing:<br />\n$query\n<br />";
        exit; }
    while (
    $temp mysql_fetch_assoc($res)){ $data[] = $temp; }

    $qry "SELECT a.Name, a.AreaType FROM basics as b, area as a WHERE a.IDArea = b.IDArea and b.IDArea = '$mycode'";
    $result mysql_query($qry);
    $type mysql_fetch_array($result);
    $mytype "my" $type['AreaType'];
    $
    $mytype $type['Name'];
    ?>
    <head>
    <?PHP
    include ($_SERVER['DOCUMENT_ROOT']."/a1/inc/head/world.php");
    include (
    $_SERVER['DOCUMENT_ROOT']."/a1/inc/head/na.php");
    include (
    $_SERVER['DOCUMENT_ROOT']."/a1/inc/head/$kon.php");
    include (
    $_SERVER['DOCUMENT_ROOT']."/a1/inc/head.php");
    ?>
    </head>
    <body>
    <h1 class="title" id="title<?=$mycode?>"><?php echo "$myname?></h1>

    <div class="divtop">
    &nbsp; <br />
    <div class="subtitle" id="sub<?=$mycode?>"><span class="subspan">
    <?php foreach ($data as $r){ print $r['NNGW']; } ?>
    </span></div>

    <div class="quotewrap" id="qwrap<?=$mycode?>">
    <div class="quote" id="q<?=$mycode?>">
    <?php reset($data); while ($r each($data)){ print $r[1]['QGW']; } ?></div>
    <div class="sig" id="sig<?=$mycode?>">
    <?php reset($data); while ($r each($data)){ print $r[1]['SigGW']; } ?></div>
    </div><!-- EndQuoteWrap -->
    <div class="toplinks" id="toplinks<?=$mycode?>"><a href="/index.php">EdRevolt Home</a> &gt; 
    <?=$sectionlink?><?php echo "$continentlink?><?php echo "$nationlink?>
    <?php 
    echo "$statelink?><?php echo "$myname?></div>
    </div><!-- EndDivTop -->
    <?php include ($_SERVER['DOCUMENT_ROOT']."/a1/inc/body.php"); ?>
    </body>
    </html>

  6. #6
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, you were right all along. I just discovered that I did something stupid - the includes with the echo statements were BELOW the echo functions, or vice versa. Anyway, I just rearranged things, and everything seems to be working now.

    I'll spend some time working on this before I ask any more questions. I think you just solved my last, or next to last major hurdle. Thanks!


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
  •