justarandomguy justarandomguy - 2 months ago 4x
MySQL Question

mysql displaying results in table format style

------------ NOTE --------------------

I would like to thank the ones for not even bothering to help me at all yet finding the time to downvote me with everything they got.

I thank you for NOT helping me out at all but still finding the time to criticize an inexperienced user who tries to make something with the little that person has.

You can continue downvoting me, it doesn't matter anymore since I requested the deleting of my account.

Finally, I would like to thank the people who DID indeed help me in the past and the few friends I made in here; a friendship that extends outside of a forum/online community.

So please stop "helping" me... I thank you

PS: David Michael you can look at the final edit of my question since this note does not refer to you guys...

I have a problem of displaying database-table in a table format, something like the following:

enter image description here

Before I present you with the code that I used I would like to describe the situation (keep in mind that I am not advanced in programming plus I worked on a sample code that I found out there... somewhere).

I have 3 different tables. They are as follows:

Table 1: id| country | id_value


1 USA 1

2 USA 1

3 UK 2

4 UK 2

5 Canada 3

Table 2: id | country | id_value | breed


1 USA cat 1

2 USA tiger 1

3 UK dog 2

4 UK snoop-dog 2

5 Canada mouse 3

Table 3: id | owner | contact | breed| id_value


1 Mike Phone1 cat 1

2 Tom Phone2 tiger 1

3 Peter email1 dog 2

4 Phil email snoop-dog 2

5 James address mouse 3

As you can observe I am using the id_value parameter to connect them.

After I managed to connect dropdown-menu1 to table 1, I can select an option and that automatically filters table 2, which allows me to chose something from the (filtered) dropdown menu 2.

The problem:
Below of it I have a button, that is supposed to do the following:
After the choices are made by the visitor/user, and tables 1/2 have been filtered, to click on the button and inside a div, or something, to display the corresponding results from table 3.

Now here are the codes:

The main_php:
(the following code was used inside the head -tags)

<script type="text/javascript">
function AjaxFunction()
var httpxml;
// Firefox, Opera 8.0+, Safari
httpxml=new XMLHttpRequest();
catch (e)
// Internet Explorer
httpxml=new ActiveXObject("Msxml2.XMLHTTP");
catch (e)
httpxml=new ActiveXObject("Microsoft.XMLHTTP");
catch (e)
alert("Your browser does not support AJAX!");
return false;
function stateck()
var myarray = JSON.parse(httpxml.responseText);
// Remove the options from 2nd dropdown list

for (i=0;i<myarray.data.length;i++)
var optn = document.createElement("OPTION");
optn.text = myarray.data[i].breed;
optn.value = myarray.data[i].id_values; // You can change this to subcategory

} // end of function stateck
var url="connect_data/dd.php";
var cat_id=document.getElementById('s1').value;

in the body:

<form name="testform" method='POST' action='mainck.php'>
Name:<input type=text name=fname>
require "connect_data/config.php";// connection to database

echo "<br>Select Category first <select name=cat id='s1' onchange=AjaxFunction();>
<option value=''>Select One</option>";

$sql="select DISTINCT id_values, country from the_countries ORDER BY country"; // Query to collect data from table

foreach ($dbo->query($sql) as $row) {
echo "<option value=$row[id_values]>$row[country]</option>";
Select Subcategory
<select name=subcat id='s2'>

</select><br><input type=submit value=submit>

the dd.php that was used (<-this php basically gets the data from the first dropdown menu, connects it to the second and back the code goes to the main.php to populate the second dropdown menu from the filtered data of table 2).

/// Preventing injection attack ////
echo "Data Error";
/// end of checking injection attack ////
require "config.php";

$sql="select DISTINCT breed,id_values from breeds_list where id_values='$cat_id' ORDER BY breed";

$main = array('data'=>$result);
echo json_encode($main);

Finally, after all that is done, according to the sample code everything is supposed to go to a file called
mainck.php and is the following one (<- please note: this part I did not touch at all because this is the point where I hit a wall).

<!doctype html public "-//w3c//dtd html 3.2//en">

<body >


echo "\$_POST['fname'] = $_POST[fname]<br>
\$_POST['cat'] = $_POST[cat]<br>
\$_POST['subcat'] = $_POST[subcat]

while (list ($key,$val) = each ($_POST)) {
echo "\$$key = $val";
echo "<br>";

<br><INPUT TYPE="button" VALUE="Back" onClick="history.go(-1);">


To conclude, all I want is to click that bloody-button and get the corresponding data (after table 1 and table 2 have been filtered) from Table 3.

I am so tired guys, you can either downvote me, or whatever, just a solution pleeeease...

----- In response to David's question for clarifications -----

(Since, in the comment section, I don't have enough characters to provide a sufficiant answer to David's question I am editing my question)

Hi David, as I tried to describe (hopefully in a not to confusing manner) there are 3 tables.

Table 1 deals with dropdown 1. Which has been resolved. Data is been displayed OK.

Table 2, gets filtered based on the choice of dropdown1 -> and displays the results of table2 in dropdown 2. Which has been resolved, and displayes data OK.

The final step (in which I am stuck for 2-3 weeks) is supposed to do the following thing:
based on the choices of dropdown 1 + dropdown 2 that have been made,
Table 3 will be filtered and with a button click to display these results below inside a div (in something like a table format as seen in the picture above).

The results that I am trying to query are from Table 3, specifically the columns | owner | contact |

To be honest I am using this sample code, because until dropdown 2 I was able to understand it and to modify it accordingly.
However, as for the final step (submit-button etc) I just do not know how to proceed anymore; I am literally stuck for 2-3 weeks (and if by chance you follow up my other questions you will see that I only ask questions in here after I have tried everything in my power - but here, I have hit a serious wall).

Finally, I do not know if the mainck.php is necessary or if it can be done otherwise; its just what the sample code suggested (and that is why, for the specific file, I did post it as it was seen in the sample code without touching it in order to avoid confusion).

I seriously hope that I am not confusing you with all my blabbering (maybe its just my frustration taking the better of me, and I apologize for that).

------------- EDIT ABOUT QUESTION (referring to David and Michael) -------

Hi guys, I believe that I owe you guys (who did indeed try to help me) an apology; and I explain myself.

I believe that my circumstance falls in the category of "when you are dealing with a problem for too long and are too close to it you might not realize the obvious" - or as Einstein stated it "if you want to solve a problem you need to ask the correct question first".

So here is the case: if you guys remember, I was trying to create 2 dropdowns from table1 and table2 and display the filtered results from Table 3 inside a div (based on the choices of dropdown1 and dropdown 2).

What I realized when trying to work with the code that Michael provided me with is the fact that I did indeed complete the section of the 2 dropdowns.

What I did also realize (based on my assumptions of course) is that in order to resolve the entire issue, what I am missing, is a mysql query that consists of two parts which are:

a) grap the filtered results from dropdown 1 and dropdown 2

b) based on these results, filter table3 and display the results inside a div below the menus.

In other words its a matter of creating a query, inside the div supported with the relevant php code.

While I did try to approach the issue with isset (examples studied from login forms etc) it did not work out (keep in mind that I am inexperienced in this department) - so I have to move along with a lot of assumptions and a lot of trial-error approaches.

Now, I am trying something like this Querying multiple SQL tables and returning the results from one or more tables in a single HTML page

And yet I still don't know if I am moving towards the right direction.

So I posted a new question here SQL query to display results from 3rd Table after Table 1 and 2 have been filtered

that I assume is the correct question to ask.

To conclude I would like to apologize, once again, to both of you, for having wasted your time due to the lack of my experience.

That being said, wish me luck guys that somehow I will be able to crack this beast...

I thank you guys both, I really am...


Did you try with jQuery and Ajax?

The first dropdown could be populated by using jQuery .get() that calls a php script in a document ready function. The second dropdown can be populated by using a .post() that call a php script with the selected value only when the user make a selection .change() in the first dropdown. A .click() event should also be applied to the Submit button and an other php .post() should be call inside of it to query the results and display it to the user in your DOM.

EDIT: Here is a short and sweet example of code using jQuery that should help you to reach your goal.

    <select id="firstDropDown"></select>
    <select id="secondDropDown"></select>
    <select id="thirdDropDown"></select>
    <input type="submit" id="submitBtn" value="Submit"></select>
    <div id="resContainer"></div>
        $(function() {
            $.get("php/yourPhpScript.php", function(data) {
                // Data received
                var json = $.parseJSON(data);

                // Build the first dropdown
                var firstDropDown;
                $.each(json, function(val) {
                    firstDropDown += "<option value=\""+val+"\">"+val+"</option>\n";

                // First dropdown change event.
                $("#firstDropDown").change(function() {
                    var val = $(this).val();
                    $.post("php/yourPhpScript.php", {
                        val: val
                    .done(function(data) {
                        // Data of second dropdown received.
                        var json = $.parseJSON(data);

                        // Build the second dropdown
                        var secondDropDown;
                        $.each(json, function(key, val) {
                            secondDropDown += "<option value=\""+key+"\">"+value+"</option>\n";

                        // Second dropdown change event.
                        $("#secondDropDown").change(function() {
                            var val = $(this).val();
                            $.post("php/yourPhpScript.php", {
                                val: val
                            .done(function(data) {
                                // Third dropdown data received
                                var json = $.parseJSON(data);

                                // Build the third dropdown
                                var thirdDropDown;
                                $.each(json, function(key, val) {
                                    thirdDropDown += "<option value=\""+key+"\">"+value+"</option>\n";
            // Add click event on the submit button
            $("#submitBtn").click(function() {
                // Here you must check if the 3 dropdowns has some values.. lets say they have some..

                $.post("php/yourPhpScript.php", {
                    val1: $("#firstDropDown").val(),
                    val2: $("#secondDropDown").val(),
                    val3: $("#thirdDropDown").val()
                .done(function(data) {
                    // Received the data results to display in a html table
                    var json = $.parseJSON(data);

                    var table;
                    // ... build your table in a $.each() loop.

                    // Display the table