SitePoint Sponsor

User Tag List

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

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

    I'm trying to fetch an article from a database with the following code on one of my pages:

    PHP Code:
    <?php
    $query 
    "
      SELECT IDArtER, ArtER1, ArtEr2
      FROM arter
      WHERE IDArtER like '
    $mycode'";
    $data = array();
    if (
    $res mysql_query($query)) { while ($temp mysql_fetch_assoc($res)) { $data[] = $temp;} }
    reset($data); while ($r each($data)) { print $r[1]['ArtER1']; }
    ?>
    Where ArtER1 is the field that contains the article. The article displays just fine except for one element - <?php echo "$myname" ?>

    I changed it to ' . $myname . ', but that doesn't work, either.

    I should explain that the above page is an include. Let's say the main page is named Alaska. Alaska includes the code I posted above, which in turn retrieves an article from my database.

    The other weird thing is that if I change ArtER1 to ArtER2, it displays nothing, even though I have an alternate article in that field.

    Any tips? Thanks.

  2. #2
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    can you do a:
    PHP Code:
      echo'<pre>';
      
    print_r($data);
     echo
    '</pre>'
    somewhere in there to verify that your data is getting put into the $data array. Your code looks a bit complicated for what you're trying to do. Doesn't look like a beginner mistake so I'll assume you have a reason for making it so complex...

    Aaron
    Aaron Brazell
    Technosailor



  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, that was a quick response; maybe a record! Very enlightening response, too.

    Quote Originally Posted by Sketch
    can you do a:
    PHP Code:
      echo'<pre>';
      
    print_r($data);
     echo
    '</pre>'
    somewhere in there to verify that your data is getting put into the $data array. Your code looks a bit complicated for what you're trying to do. Doesn't look like a beginner mistake so I'll assume you have a reason for making it so complex...

    Aaron
    Yikes - I thought that WAS simple! Actually, I am a beginner. I've been working on this project for weeks. I learned how to display dynamic tables fairly recently and learned how to display bits of data from my table in scattered elements just the other day. I asked if there was a way to simplify my code, and someone gave me a tip that let me cut it in half.

    To put it in perspective, think of my main page as Alaska again. There's virtually nothing in the body section except an include link to includes/BODY.php

    BODY.php in turn includes the top section (banner, title, quote, etc.) - includes/TOP.php

    I first started experimenting with my latest query on the TOP include page. Below is the code from that page. Note that I'm drawing from a table named "basics."

    PHP Code:
    <?php
    $todayDate 
    date("m-d-Y");
    echo 
    '<h1 class="title" id="title' $mycode '">' $myname '</h1>'
    ?>

    <div class="divtop">&nbsp;<br />
    <?php
      
    echo '<div class="subtitle" id="sub' $mycode '">';
    $query "
      SELECT IDArea, Name, NNER, QER, SigER
      FROM basics 
      WHERE IDArea like '
    $mycode'";
    $data = array();

    if (
    $res mysql_query($query)) { while ($temp mysql_fetch_assoc($res)) { $data[] = $temp;} }
    foreach (
    $data as $r) { print $r['NNER']; }
      echo 
    '</div>
      <div class="quotewrap" id="qwrap' 
    $mycode '">
        <div class="quote" id="q' 
    $mycode '">';

    reset($data); while ($r each($data)) { print $r[1]['QER']; }
        echo 
    '</div>';
        echo 
    '<div class="sig" id="sig' $mycode '">';
    reset($data); while ($r each($data)) { print $r[1]['SigER']; }
        
        echo 
    '</div>
      </div>'
    ;
    ?>
    <?php
    foreach ($data as $r) { print $r['AreaID']; }
    ?>    
        
      <div class="toplinks" id="toplinks<?php echo "$mycode?>"><a href="/index.php">EdRevolt
          Home</a> &gt; <?php echo "$sectionlink?><?php echo "$continentlink?><?php echo "$countrylink?><?php echo "$mylink?>
      </div>
      <div class="region"><span class="region2">MY REGIONS &gt; <a href="http://www.geoworld.org/<?php echo "$kont?>/regions/index.php#<?php echo "$regioncode?>">
      <?php echo "$myregion?></a><?php echo "$myregion2?><?php echo "$myregion3?>
      </div>
    </div>
    Just an hour ago or so, I decided to try to query another table - "ArtER" - on my BODY include. This is what that page looks like:

    PHP Code:
    <?php
    $todayDate 
    date("m-d-Y");
    ?>
    <div class="body2" id="body2<?php echo "$mycode?>">
      <div class="body3" id="body3<?php echo "$mycode?>">



    // * * * * * * Here's the link to the top include I posted above * * * * * *
        <?php include ($_SERVER['DOCUMENT_ROOT']."/includes/top.php"); ?>

      


      <table id="tablemain" cellspacing="0" cellpadding="0">
          <tr class="rowmain">
            <td class="tdleft" id="tdleft<?php echo "$mycode?>">&nbsp;</td>
            <td class="tdcenter" id="tdcenter<?php echo "$mycode?>">



    // * * * * And here's where I queried another table - arter.
     
    <?php
    $query 
    "
      SELECT IDArtER, ArtER1, ArtEr2
      FROM arter
      WHERE IDArtER like '
    $mycode'";
    $data = array();
    if (
    $res mysql_query($query)) { while ($temp mysql_fetch_assoc($res)) { $data[] = $temp;} }
    reset($data); while ($r each($data)) { print $r[1]['ArtER1']; }

    // * * * * * And here's where I inserted the code you gave me * * * * *

    echo'<pre>';
      
    print_r($data);
    echo
    '</pre>';

    ?> 




            </td>
            <td class="tdright" id="tdright<?php echo "$mycode?>">&nbsp;</td>
          </tr>
        </table>
        <!--END TABLEMAIN-->
        <?php include ($_SERVER['DOCUMENT_ROOT']."/includes/footer.php"); ?>
      </div>
      <!--END BODY3-->
    </div>
    <!--END BODY2-->
    <?php include ($_SERVER['DOCUMENT_ROOT']."/includes/navmenu.php"); ?>
    <?php 
    include ($_SERVER['DOCUMENT_ROOT']."/includes/banner.php"); ?>
    And when I inserted the code you gave me, the second article I've been trying to retrieve suddenly appeared, though the page looks a little screwy.

  4. #4
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why don't you simplify this bit into:
    PHP Code:
     $query "
       SELECT IDArtER, ArtER1, ArtEr2
       FROM arter
       WHERE IDArtER like '
    $mycode'";
     if (
    $res mysql_query($query)) 
       { 
       
    $temp mysql_fetch_array($res)
       print 
    $temp['ArtER1']; 
       } 
    Aaron Brazell
    Technosailor




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
  •