1. I am trying to use this code snippet, to learn how to create a dropdown menu, populated by a mysql database.
  2. I have worked out the kinks of the database credentials.
  3. I have created and populated a test database.
  4. This does actually work, now that it is morning time and the sun is coming up already..
  5. I do get mostly a blank screen, but with a drop down menu (in the top left corner), with the 2 fields i inserted in my database shown.. So stoked there!
  6. BUT! I receive the error @ the botoom of this post
  7. Furthermore... the point of doing this excerise, is to replace the hand made table i use for my ad space, which is popluated by java scripts. So i can move onto table that poplutaes istelf from a database.
  8. Iam going to post his twice i suppose since its being retarded and wont let me post with my code smippet.. idk maybe i need sleep?
    ERROR=
    Notice: Undefined variable: partner_block in /var/www/test/test-php-dropdownmenu.php on line 22

Recommended Answers

All 27 Replies

<?php
ini_set('display_errors', 1);
error_reporting(~0);

    ## define your database connection credentials
    mysql_connect("localhost","user","password") or die(mysql_error());
    mysql_select_db("databasename") or die(mysql_error());
    $partners = mysql_query("SELECT * FROM partners")
    or die(mysql_error());
    while($partner = mysql_fetch_array( $partners ))
    {
    $partner_id = $partner['id'];
    $partner_name = $partner['name'];
    ## Now, let's assign the partner inside the option form tags.
    ## the partner named bundagle and somewhere should be now inside the option tags.
    $partner_block .= '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';
    } ## end while loop
    ## now lets show the result from the table partners.
    ?>
    <label for="partner">Partner</label>
    <select name="partnerID"><?php echo $partner_block; ?></select>

You are appending a string to a variable $partner_block that is not defined anywhere else (at least not in the code you posted):

$partner_block .= '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';

thanks for Posting!
thats probably because i barely know what i am doing...
how do i fix it?

If the variable has not been declared yet, you can not append to it. You can asign to it (with asigning you declare the variable). So just remove the dot (appending operator):

$partner_block = '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';

That will remove the error in line 22 but I do not know if that is all that is needed for your code to work correctly. Tell us how it goes.

error is cleared... word!
next issue... i only populated the drop down menu with 1 of the 2 rows in the database.
Also to the left of the menu, was the printed text
Partner
fyi you have all the "PHP" code i have...
this php file is successfuly being called from my index.html file.

OK, so put back the dot you removed and declare the $partner_block variable before the while loop (so you can later append to it):

$partner_block = '';
while($partner = mysql_fetch_array( $partners ))
{
    $partner_id = $partner['id'];
    $partner_name = $partner['name'];
    ## Now, let's assign the partner inside the option form tags.
    ## the partner named bundagle and somewhere should be now inside the option tags.
    $partner_block .= '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';
} ## end while loop

i have 3 files in the /var/www/test/.htaccess & index.html & test-php-dropdownmenu.php
here are the contents of test-php-dropdownmenu.php

<?php
ini_set('display_errors', 1);
error_reporting(~0);

    ## define your database connection credentials
    mysql_connect("localhost","root","chronic420!") or die(mysql_error());
    mysql_select_db("aromadata") or die(mysql_error());
    $partners = mysql_query("SELECT * FROM partners")
    or die(mysql_error());
    while($partner = mysql_fetch_array( $partners ))
    {
    $partner_id = $partner['id'];
    $partner_name = $partner['name'];
    ## Now, let's assign the partner inside the option form tags.
    ## the partner named bundagle and somewhere should be now inside the option tags.
    $partner_block = '<OPTION value="'.$partner_id.'">'.$partner_name.'</OPTION>';
    } ## end while loop
    ## now lets show the result from the table partners.
    ?>
    <label for="partner">Partner</label>
    <select name="partnerID"><?php echo $partner_block; ?></select>

here are the contents of .htaccess

#deny from all
DirectoryIndex index.html
Options +FollowSymLinks
RewriteEngine on
#RewriteBase /var/www/test/
php_value display_errors on

here are the contents of index.html

<html>
<body>
<?php
if(isset($_POST['submit'])) {
foo();
}
?>
<?php require ('test-php-dropdownmenu.php'); ?>
<form action="test-php-dropdownmenu.php"]"method="post">
<input type="submit" name="submit" value="Click Me">
</form>
</body>
</html>

Everything is alpha stage, running on a fresh install of linx mint 14 on a laptop, with a lamp-server^ installed & phpmyadmin

See my post above.

Thanks Bro! So far so good... now it's populating with both the rows... Stoked!
It still displays the text Partners to the left of the menu... im assuming its supposed to be there. Also that it is coming from <label for="partner">Partner</label>.
Question #1: is that correct?
Question #1-B: May i use things like <h1> like i can with <a href= ?
I have a new question now? Question 2?
i had spent a lot of time hand making tables based on 125px X 125px squares. Using the colspan & rowspan attributes to make bigger images fit and align properly.
The question here is... IS there a nice and easy way to re-draw said tables, based on the output, resulting from choosing an option from the drop down menu???
example...

option 1 has 8 125 X 125 images (They line up from left to right)
option 2 has 16 125 X 125 images (They line up from left to right & use 2 rows)
option 3 has 8 125 X 125 images & 6 250 X 250 images (They line up from left to right & use 4 rows)*

Any Idears anybody?
(FYI not code but it forced me to comply)

It still displays the text Partners to the left of the menu... im assuming its supposed to be there. Also that it is coming from <label for="partner">Partner</label>.

Yes.

Question #1-B: May i use things like <h1> like i can with <a href= ?

Sorry, I do not understand the question. Please explain.

IS there a nice and easy way to re-draw said tables, based on the output,

There is nice and easy way if you do not mind reloading the page. Just send the new values using a form and GET or POST method.

If you do now want to reload the page you can use ayax but that is not that easy anymore. You should be familiar with ajax for this.

Ont he note of <h1>... i tred and it worked... now i have a giant text next to my menu...
The best further explanation i can give... is to look at the footer of www.aromacomputers.com.
Each category of ads contains differnet amounts of different sizes of different pictures.
Each category is going to be an option to select from the drop down box we just made work.
I want to replace whats in my current footer, with what i am building out now.
So i can basically hide alll the extras images of irrelevent ads. While also preventing so many ads from loading unless they choose see all of course.
Also i want to add a search bar to this... so you could type say "hp" & get hp ads...
My issue is that it took me forever to make the tables & if i had to do that each time i added another ad, its going to drive me crazy not to mention consume all my time.
So I want to populate the javascripts in the cells, from a database.
I also want the ads to display properly, by having PHP magically re-draw my tables.

If you do now want to reload the page you can use ayax but that is not that easy anymore. You should be familiar with ajax for this.

My live site does use ajax... not that i set it up...
i also have the ajax-animator-wave which excites me we may beat adobe after all ;)

Sounds easy... but i have already read what ive found about those commands... but idk???
Can you please explain more? or maybe even provide a basic example?
Playing with it will help me undertstand?
& can you change the heading of this thread to "Create Ad Space" instead of "create a mysql polulated dropdown menu"

Just send the new values using a form and GET or POST method.

You have to be familiar with ajax to use it. The simplest way is to use jquery. If you are not familiar with it then I strongly suggest you learn it first, otherwise you will be dependend on others to help you with each issue and noone can guarantee a quick response. And easiest way to help someone is to see the code.

So I want to populate the javascripts in the cells, from a database.

What is the javascript code?

I also want the ads to display properly, by having PHP magically re-draw my tables

You can do that easily with PHP. What is the code for the table?

And easiest way to help someone is to see the code.

Its a 40+ MB tar.gz i can ftp you if you let me... for the template that runs my live site.

What is the javascript code?

<script type="text/javascript" language="javascript" src="http://www.redacted.com/redactedredactedredacted?target=_blank&mouseover=Y"></script>

What is the code for the table?
Multiple tables... this is a sample...

<table align="center" cellpadding="0" cellspacing="0">
<tr>
</tr>
<tr align="center">
<td rowspan=2 colspan=2 height="125" width="125"><script type="text/javascript" language="javascript" src="http://www.redacted.net/redactedredactedredacted?target=_blank&mouseover=Y"></script></td>
<td rowspan=2 colspan=2 height="125" width="125"><script type="text/javascript" language="javascript" src="http://www.redacted.net/redactedredactedredacted?target=_blank&mouseover=Y"></script></td>
<td height="125" width="125"><script type="text/javascript" language="javascript" src="http://www.redacted.com/redactedredactedredacted?target=_blank&mouseover=Y"></script></td>
<td height="125" width="125"><script type="text/javascript" language="javascript" src="http://www.redacted.net/redactedredactedredacted?target=_blank&mouseover=Y"></script></td>
</tr>
<tr align="center">
<td height="125" width="125"><script type="text/javascript" language="javascript" src="http://www.redacted.com/redactedredactedredacted?target=_blank&mouseover=Y"></script></td>
<td height="125" width="125"><script type="text/javascript" language="javascript" src="http://www.redacted.net/redactedredactedredacted?target=_blank&mouseover=Y"></script></td>
</tr>
</table>

shoot... i should add that...
on my index.html page, all that displays is the button thats says "Click Me".
when i click it, the page changes to a different page. So i think that covers the page needing to be refreshed anyways.. 2 birds with one stone.
except i do need to figure out how to...
define what page it takes you to, and where that page is.

To generate table just do something like:

$jsCode = 'http://www.redacted.net/redactedredactedredacted?target=_blank&mouseover=Y';

$spannedRow  = '<td rowspan=2 colspan=2 height="125" width="125">';
$spannedRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$spannedRow .= '"></script></td>';

$normalRow  = '<td height="125" width="125">';
$normalRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$normalRow .= '"></script></td>';

echo '<table align="center" cellpadding="0" cellspacing="0">';

// if you read data from the sb do the while loop otherwise do the for loop
while(<reading rows == true>) {

    echo '<tr>';
    echo $spannedRow;
    echo $spannedRow;
    echo $normalRow;
    echo $normalRow;
    echo $normalRow;
    echo $normalRow;
    echo '</tr>';
}

echo '</table>';

This is just an example. You have to apply your logic to it.

can you change the heading of this thread to "Create Ad Space" instead of "create a mysql polulated dropdown menu"

I cant't do that, I am not the admin.

Very many questions in such a short time. I am getting lost :-)

this sounds awesome & just what i need... THANKS
Ummm... can i pre-input all the variables (like height, width, rows, columns) as entries in my database. AKA when i get the new links enter them only in the database, all fields.
how would the above code change in order to do so?

Its a 40+ MB tar.gz i can ftp you if you let me... for the template that runs my live site.

No need to send entire site code. With each question just post the relevant code snippet.

except i do need to figure out how to define what page it takes you to, and where that page is.

<?php
// define the target page using some logic
$target_page = 'some_page.php';
?>

<form method="post" action="<?php echo $target_page; ?>">
<input type="submit" name="submit" value="Click me">
</form>

For images you do not have to store the sizez (you could if you wish). You can get them using getimagesize function instead.

// calculate the width and the height or read them from db
$width = ...
$height = ...

// use the dimensions in the code
$spannedRow = '<td rowspan=2 colspan=2 height="' . $height . '" width="' . $width . '">';
$spannedRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$spannedRow .= '"></script></td>';
$normalRow = '<td height="' . $height . '" width="' . $width . '">';
$normalRow .= '<script type="text/javascript" language="javascript" src="' . $jsCode;
$normalRow .= '"></script></td>';

soory about that... i havent sleptbecause i started working on this @ midnight & have been going turbobooster since then makin progress.
Made my 1st tables ;) & my 1st excutable php buttton embedded in html ;) thats a good one (re usable)

i havent sleptbecause i started working on this @ midnigh

No worries mate. Just take it easy. Sometimes it is good to take a break for 10 mins or so.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.