Jump to content

How to add a break into a PHP pull down list that pulls data from an SQL DB?


Matt Ridge

Recommended Posts

I am getting an error on line 3 for the original code, so I need help there, but what I do have a question on is this:

 

http://kaboomlabs.com/PDI/test2.php

 


<?php
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
	$result = $mysqli_query("SELECT * FROM comp");

      echo "<SELECT name='comp'>\n";
      while($row = $result->fetch_assoc()) {
         echo "<option value='{$row['name']}</option>\n";   
      }
      echo "</select>\n";

      $result_close();

      ?>

 

What  I am attempting to do is two fold.

 

1. Using PHP create a pull down menu that grabs data from the database.

 

2.  Have something in where after line three there is a dotted break, and then the rest of the list is shown.

 

There are over 150 entities that go into this database, so the top 3 are going to be the most used, the rest are going to be in alphabetical order.

 

Now the database has a auto-increment numbering system, the company name, address, phone number, and email if possible. I only want it to show the company name.

 

Is this possible at all? 

Link to comment
Share on other sites

Keep  a count of the items as you output them, and output your separator at the right time.

 

$counter=0;
while($row = $result->fetch_assoc()) {
    echo "<option value='{$row['name']}</option>\n";
    if (++$counter == 3){
       echo '<option>-------------------</option>';
    }
}

 

 

Your error is due to you missing a ; on your connect line.

 

Link to comment
Share on other sites

Keep  a count of the items as you output them, and output your separator at the right time.

 

$counter=0;
while($row = $result->fetch_assoc()) {
    echo "<option value='{$row['name']}</option>\n";
    if (++$counter == 3){
       echo '<option>-------------------</option>';
    }
}

 

 

Your error is due to you missing a ; on your connect line.

 

 

Thanks, and I needed someone to look it over for me, staring at the same code all the time makes me go insane.

 

This code would go between line 7 and 8 right?

Link to comment
Share on other sites

is that your real code?  ... asking because I see some intent to use mysqli OOP style coding mixed with Procedural style... mixing like that is not going to work.

 

here http://php.net/manual/en/mysqli.query.php you can see clear examples of both styles

 

in your code are wrongly used...

-  mysqli_connect

- $mysqli_query

- $result->fetch_assoc()

- $result_close()

Link to comment
Share on other sites

is that your real code?  ... asking because I see some intent to use mysqli OOP style coding mixed with Procedural style... mixing like that is not going to work.

 

here http://php.net/manual/en/mysqli.query.php you can see clear examples of both styles

 

in your code are wrongly used...

-  mysqli_connect

- $mysqli_query

- $result->fetch_assoc()

- $result_close()

 

 

I got it from here, http://www.phpfreaks.com/forums/index.php?topic=183307.msg819380#msg819380

 

So how would you fix it, just asking because I'm lost.

Link to comment
Share on other sites

read the examples in the link that I did provide.... and by the way... the link that you are referring to has also examples that differ from what you wrote in your code

 

It differs because when I used the code the way it was inputted originally I was getting more errors on it because of other reasons. 

 

Thanks, I'll look into what you posted though.

Link to comment
Share on other sites

It differs because when I used the code the way it was inputted originally I was getting more errors on it because of other reasons. 

 

No, it differs because you are using the basic mysqli sentences incorrectly...  let me help you pointing out the errors using the last post of the thread that you used as an example (no matter if that example is ok or not for your objectives)

Example code from the referred link... comments added... It is using OOP Style in the whole script

<?
      $mysqli = new mysqli('localhost','root','newr00t'); // Can you see how this line differs from yours?
      $mysqli->select_db('orders');

      $result = $mysqli->query("SELECT * FROM user"); // This is how he execute the query... compare this line with yours

      echo "<SELECT name='user'>\n";
      while($row = $result->fetch_assoc()) {
         echo "<option value='{$row['userid']}'>{$row['name']}</option>\n";   
      }
      echo "</select>\n";

      $result->close();  // This is how the result set is closed.... compare it  with your closing line

      ?>

 

hope this little comparison help you with your code

Link to comment
Share on other sites

It differs because when I used the code the way it was inputted originally I was getting more errors on it because of other reasons. 

 

No, it differs because you are using the basic mysqli sentences incorrectly...  let me help you pointing out the errors using the last post of the thread that you used as an example (no matter if that example is ok or not for your objectives)

Example code from the referred link... comments added... It is using OOP Style in the whole script

<?
      $mysqli = new mysqli('localhost','root','newr00t'); // Can you see how this line differs from yours?
      $mysqli->select_db('orders');

      $result = $mysqli->query("SELECT * FROM user"); // This is how he execute the query... compare this line with yours

      echo "<SELECT name='user'>\n";
      while($row = $result->fetch_assoc()) {
         echo "<option value='{$row['userid']}'>{$row['name']}</option>\n";   
      }
      echo "</select>\n";

      $result->close();  // This is how the result set is closed.... compare it  with your closing line

      ?>

 

hope this little comparison help you with your code

 

Actually it does a lot, thanks...

 

If I wanted to add this into the code, would I enter this between line 13 and 14?

Link to comment
Share on other sites

no idea which are your lines 13 - 14... post your updated code first  .... but kitchen already show you how to incorporate the dotted separation, however, and just a comment for now ... here is what you defined as your goals:

 

There are over 150 entities that go into this database, so the top 3 are going to be the most used, the rest are going to be in alphabetical order.

 

Now the database has a auto-increment numbering system, the company name, address, phone number, and email if possible. I only want it to show the company name.

 

Which is your criteria to define which ones are the 3 most used among the 150 or more records in your table?... you need to have a mechanism to separate those 3 from the rest (because the rest need to be ordered alphabetical as you said).... think about that for now.

Link to comment
Share on other sites

no idea which are your lines 13 - 14... post your updated code first  .... but kitchen already show you how to incorporate the dotted separation, however, and just a comment for now ... here is what you defined as your goals:

 

There are over 150 entities that go into this database, so the top 3 are going to be the most used, the rest are going to be in alphabetical order.

 

Now the database has a auto-increment numbering system, the company name, address, phone number, and email if possible. I only want it to show the company name.

 

Which is your criteria to define which ones are the 3 most used among the 150 or more records in your table?... you need to have a mechanism to separate those 3 from the rest (because the rest need to be ordered alphabetical as you said).... think about that for now.

 

Already have, lines ID's 1-3 are the top 3... the id's 4-160 are the rest.

 

http://kaboomlabs.com/PDI/test2.php

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.