Matt Ridge Posted December 22, 2011 Share Posted December 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/ Share on other sites More sharing options...
kicken Posted December 22, 2011 Share Posted December 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300550 Share on other sites More sharing options...
Matt Ridge Posted December 22, 2011 Author Share Posted December 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300554 Share on other sites More sharing options...
Pikachu2000 Posted December 22, 2011 Share Posted December 22, 2011 The option tags in the loop are badly malformed, too. Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300555 Share on other sites More sharing options...
Matt Ridge Posted December 22, 2011 Author Share Posted December 22, 2011 The option tags in the loop are badly malformed, too. What do you mean? Sorry, but I got this code originally from this site and made it into my own... so I am going to by twice removed original code. Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300556 Share on other sites More sharing options...
Pikachu2000 Posted December 22, 2011 Share Posted December 22, 2011 Assuming $row['name'] holds the value 'Bob', the code would output invalid markup like this: <option value='Bob</option> Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300562 Share on other sites More sharing options...
Matt Ridge Posted December 22, 2011 Author Share Posted December 22, 2011 Assuming $row['name'] holds the value 'Bob', the code would output invalid markup like this: <option value='Bob</option> but what would dictate it is an invalid markup? I only ask, because your code is missing a '... Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300565 Share on other sites More sharing options...
mikosiko Posted December 22, 2011 Share Posted December 22, 2011 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() Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300567 Share on other sites More sharing options...
Pikachu2000 Posted December 22, 2011 Share Posted December 22, 2011 That's your code, not mine; I copied and pasted it. With the value 'Bob' assigned to $row['name'], that's the exact output it generates. $row['name'] = 'Bob'; echo "<option value='{$row['name']}</option>\n"; Returns: <option value='Bob</option> Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300568 Share on other sites More sharing options...
Matt Ridge Posted December 22, 2011 Author Share Posted December 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300570 Share on other sites More sharing options...
mikosiko Posted December 22, 2011 Share Posted December 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300571 Share on other sites More sharing options...
Matt Ridge Posted December 22, 2011 Author Share Posted December 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300574 Share on other sites More sharing options...
mikosiko Posted December 22, 2011 Share Posted December 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300580 Share on other sites More sharing options...
Matt Ridge Posted December 22, 2011 Author Share Posted December 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300583 Share on other sites More sharing options...
mikosiko Posted December 22, 2011 Share Posted December 22, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300590 Share on other sites More sharing options...
Matt Ridge Posted December 22, 2011 Author Share Posted December 22, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/253692-how-to-add-a-break-into-a-php-pull-down-list-that-pulls-data-from-an-sql-db/#findComment-1300600 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.