I am modifiying some templates and scripts used in the vbulletin message board system. The php scripts call in the needed templates and process them via an "eval" function.

I'm trying to modify a template to include a select button that is derived from the contents of a db. I have included the following at the beginning of the php script that calls the template:

####1#########
$citydata = mysql_fetch_array($cities);
#############

As a test, the following is included in the php script to test the validity of the connection. It works:

####2#########
?>
<SELECT NAME="city">
<? while($data = mysql_fetch_array($cities)) {
echo("
<OPTION>$data[state] - $data[cityname]
"); } ?>
</SELECT>
<?
#############

Of course, the above puts the select menu in an inappropriate place, but it was just a test.

Therefore, on the template, I've added this:

####3#########
<SELECT NAME="clubcity">
while($citydata = mysql_fetch_array($cities))
<OPTION>$citydata[state] - $citydata[cityname]
</SELECT>
#############

That code produces a select menu with only the first item in the array. It's accessing the db, but not creating the entire dropdown menu.

The line in the php script that calls in the template is this:

####4#########
eval("dooutput(\"".gettemplate("memberlistcitysearch")."\");");
#############

The following is the code output on the html page produced:

####5#########
<SELECT NAME="clubcity">
while(Array = mysql_fetch_array(Resource id #2))
<OPTION>AB - Alberta General
</SELECT>
#############

Clearly, I have either forgotten something in the coding of #3# above, or the eval function in #4# is doing something I don't understand. Or both, since by now my eyes are really crossed.

Thanks in advance,

Paul Cardin

    It appears as though #3# is included in a block which is printing out to the standard output (the generated HTML page). Therefore, the while function is being printed as plain text and the variables $citydata and $cities are parsed for their value and being printed as "Array" and "Resource id #2" respectively. You need to get your while loop out of the output block and make it execute as code. I have no idea how you're outputting #3# so I don't know how to stop it, but if you're using echo <<< SOMEWORD
    ...
    SOMEWORD;

    you just need to put the SOMEWORD; line after the <SELECT> line is output in #3# so the while line can be evaluated as PHP code.
    The reason #2# works is that the <SELECT> lines are being output outside of PHP, hence the ?> immediately before it and the <? immediately after. Hope this makes sense. If you give me more code I can help you further.

    Adam

      Adam:

      Below is the entire code for the memberlistcitysearch template. The overall purpose of this code is to create a dropdown select menu with for a form and to use the 1850 records in a db for the options.
      The template is called up by a php script (along with dozens of others) and is processed by an eval function.

      Thanks.

      Paul

      {htmldoctype}

      <head>
      <title>$bbtitle - Members List - Advanced Search</title>
      $headinclude
      </head>
      <body>
      $header

      <table cellpadding="2" cellspacing="0" border="0" width="{contenttablewidth}" {tableinvisibleextra} align="center">
      <tr>
      <td width="100%"><img src="{imagesfolder}/vb_bullet.gif" alt="" border="0" align="absmiddle">
      <normalfont><b><a href="index.php?s=$session[sessionhash]">$bbtitle</a> &gt;
      <a href="memberlist.php?s=$session[sessionhash]">Members List</a> &gt;
      Advanced Search</b></normalfont></td>
      </tr>
      </table>

      <br>

      <table cellpadding="{tableouterborderwidth}" cellspacing="0" border="0" bgcolor="{tablebordercolor}" {tableouterextra} width="{contenttablewidth}" align="center"><tr><td>
      <table cellpadding="4" cellspacing="{tableinnerborderwidth}" border="0" {tableinnerextra} width="100%">
      <tr align="center">

      <td bgcolor="{secondaltcolor}"><smallfont><a href="membercities.php?s=$session[sessionhash]&action=search"><b>Search by Tip Club Cities</b></a></smallfont></td>

      <td bgcolor="{firstaltcolor}"><smallfont><a href="memberlist.php?s=$session[sessionhash]&action=search"><b>Advanced Search</b></a></smallfont></td>
      <td bgcolor="{secondaltcolor}"><smallfont><a href="memberlist.php?s=$session[sessionhash]&what=topposters&perpage=$memberlisttopposters"><b>Top $memberlisttopposters Posters</b></a></smallfont></td>
      <td bgcolor="{secondaltcolor}"><smallfont><a href="memberlist.php?s=$session[sessionhash]"><b>List Alphabetically</b></a></smallfont></td>
      <td bgcolor="{secondaltcolor}"><smallfont><a href="memberlist.php?s=$session[sessionhash]&what=datejoined"><b>List by Date Joined</b></a></smallfont></td>

      </tr>
      </table>
      </td></tr></table>

      <br>

      <form action="membercities.php" method="post">
      <input type="hidden" name="s" value="$session[sessionhash]">
      <input type="hidden" name="action" value="getall">

      <table cellpadding="{tableouterborderwidth}" cellspacing="0" border="0" bgcolor="{tablebordercolor}" {tableouterextra} width="{contenttablewidth}" align="center"><tr><td>
      <table cellpadding="4" cellspacing="{tableinnerborderwidth}" border="0" {tableinnerextra} width="100%">
      <tr>
      <td bgcolor="{tableheadbgcolor}" colspan="2"><normalfont color="{tableheadtextcolor}"><b>Show users by Internet Tip Club City</b></normalfont></td>
      </tr>
      <tr>
      <td bgcolor="{firstaltcolor}"><normalfont><b>Select Internet Tip Club City</b></normalfont></td>
      <td bgcolor="{firstaltcolor}"><normalfont>

      <SELECT NAME="clubcity">

      while($citydata = mysql_fetch_array($cities))

      <OPTION>$citydata[state] - $citydata[cityname]

      </SELECT>
      </TD>
      </TR>

         <TR>

      </normalfont></td>

      </tr>
      <tr>
      <td bgcolor="{tableheadbgcolor}" colspan="2"><normalfont color="{tableheadtextcolor}"><b>Options</b></normalfont></td>
      </tr>
      <tr>
      <td bgcolor="{secondaltcolor}"><normalfont><b>Order by:</b></normalfont></td>
      <td bgcolor="{secondaltcolor}"><normalfont><select name="orderby">
      <option selected>username</option>
      <option>joindate</option>
      <option>lastpost</option>
      <option>posts</option>
      </select>
      <select name="direction">
      <option value="">in ascending order</option>
      <option value="DESC">in descending order</option>
      </select></normalfont></td>
      </tr>
      <tr>
      <td bgcolor="{firstaltcolor}"><normalfont><b>Number of records per page:</b></normalfont></td>
      <td bgcolor="{firstaltcolor}"><input type="text" class="bginput" size="35" name="perpage" value="$memberlistperpage"></td>
      </tr>
      </table>
      </td></tr></table>

      <br>

      <table cellpadding="2" cellspacing="0" border="0" width="{contenttablewidth}" {tableinvisibleextra} align="center">
      <tr>
      <td align="center"><normalfont>
      <input type="submit" class="bginput" name="submit" value="Submit" accesskey="s">
      <input type="reset" class="bginput" name="reset" value="Reset"></normalfont>
      </td>
      </tr>
      </table>

      </form>

      $footer
      </body>

        I think I understand now. The issue I see is that you're trying to put executable code into a template. This needs to be used as a template which is dumped in whole to the std out, not data generating code. I think your easiest solution will be to run the while loop in the code that calls this template and store the results in a variable. You can then simply reference that variable in place of the while loop in the template.

        // goes into parent code that calls template
        $option_pulldown = "";
        while($citydata = mysql_fetch_array($cities))
        {
        $option_pulldown .= "<OPTION>$citydata[state] - $citydata[cityname]\n";
        }

        Put this code in the calling file before the template is evaluated and then replace the above lines in the template with $option_pulldown and you should get the desired results. Hope that helps!

        Adam

          Adam:

          Thanks, that was the problem and the solution. My only remaining item is that the first record in the db $cities is not showing up in the pull down. I'm not sure if it's the while loop that isn't getting it or if it's some other reason.

          Actually, when I looked deeper, the coding in the php script that I was using as a test (that I said was working) wasn't putting it in either.

          Paul

            The while loop shouldn't cause that to happen. Either the pointer in the restult set is set to position 1 instead of 0 due to a previous read before the while loop started or there is a problem with the original query.

            If the pointer is off from a previous mysql_fetch you can do this:

            mysql_data_seek($cities, 0);

            before the while loop and see if that gets you the first entry of the db.

            Adam

              Adam:

              There was a previous fetch in the script left from when I was splitting things between the script and the template. I removed it and things are perfect.

              Thank you very much.

              Paul

                Write a Reply...