Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question [HELP] mysql and json encode

    Code:
    <?php
    	$db = new mysqli('localhost', 'root', 'root', 'elitedb') or die("An error occured. " .mysql_error());
    	$query = "select categoryid, categoryname from tcategory";
    	$result = $db->query($query);
    
    	while($row = $result->fetch_assoc()){
    		$categories[] = array("id" => $row['categoryid'], "val" => $row['categoryname']);
    	}
    
    	$query = "select subcategoryid, subcategoryname, parentcategoryid from tsubcategory";
    	$result = $db->query($query);
    
    	while($row = $result->fetch_assoc()){
    		$subcategories[$row['parentcategoryid']][] = array("id" => $row['subcategoryid'], "val" => $row['subcategoryname']);
    	}
    
    	$jsonCategories = json_encode($categories);
    	$jsonSubcategories = json_encode($subcategories);
    	
    	$query = "select carvendorid, carvendorname from tcarvendor";
    	$result = $db->query($query);
    	
    	while($row = $result->fetch_assoc()){
    		$carvendor[] = array("id" => $row['carvendorid'], "val" => $row['carvendorname']);
    	}
    	
    	$query = "select carid, carname, carvendorid from tcar";
    	$result = $db->query($query);
    	
    	while($row = $result->fetch_assoc()){
    		$car[$row['carvendorid']][] = array("id" => $row['carid'], "val" => $row['carname']);
    	}
    	
    	$jsonCarvendor = json_encode($carvendor);
    	$jsonCar = json_encode($car);
    	
    	/*================*/
    	
    	$query = "select tcaryear.yearid, tyear.year, tcaryear.carid from tcaryear inner join tyear on tyear.yearid = tcaryear.yearid";
    	$result = $db->query($query);
    	
    	while($row = $result->fetch_assoc()){
    		$year[$row['tcaryear.carid']][] = array("id" => $row['tcaryear.yearid'], "val" => $row['tyear.year']);
    	}
    	
    	$jsonYear = json_encode($year);
    ?>
    
    <html>
    <head>
    	<script type='text/javascript'>
            <?php
                echo "var categories = $jsonCategories; \n";
                echo "var subcategories = $jsonSubcategories; \n";
    			echo "var carvendor = $jsonCarvendor; \n";
    			echo "var car = $jsonCar; \n";
    			echo "var year = $jsonYear; \n";
            ?>
    		
    		window.onload = function(){
    			loadCategories();
    			loadCarvendor();
    		}
    		
            function loadCategories(){
                var categorySelect = document.getElementById("category");
                categorySelect.onchange = updateSubCategories;
                for(var a = 0; a < categories.length; a++){
                    categorySelect.options[a] = new Option(categories[a].val, categories[a].id);          
                }
            }
            function updateSubCategories(){
                var categorySelect = this;
                var categoryid = this.value;
                var subcategorySelect = document.getElementById("subcategory");
                subcategorySelect.options.length = 0;
                for(var a = 0; a < subcategories[categoryid].length; a++){
                    subcategorySelect.options[a] = new Option(subcategories[categoryid][a].val, subcategories[categoryid][a].id);
                }
            }
    		function loadCarvendor(){
    			var carvendorSelect = document.getElementById("carvendor");
    			carvendorSelect.onchange = updateCar;
    			for(var b = 0; b < carvendor.length; b++){
    				carvendorSelect.options[b] = new Option(carvendor[b].val, carvendor[b].id);
    			}
    		}
    		function updateCar(){
    			var carvendorSelect = this;
    			var carvendorid = this.value;
    			var carSelect = document.getElementById("car");
    			carSelect.onchange = updateYear;
    			carSelect.options.length = 0;
    			for(var b = 0; b < car[carvendorid].length; b++){
    				carSelect.options[b] = new Option(car[carvendorid][b].val, car[carvendorid][b].id);
    			}
    		}
    		function updateYear(){
    			var carSelect = this;
    			var carid = this.value;
    			var yearSelect = document.getElementById("year");
    			yearSelect.options.length = 0;
    			for(var b = 0; b < year[carid].length; b++){
    				yearSelect.options[b] = new Option(year[carid][b].val, year[carid][b].id);
    			}
    		}
        </script>
    </head>
    <body>
        <form id="addstock" method="post" action="index.php?module=prosesAddStock">
            <table border="1">
                <tr>
                    <td> Code Barang </td>
                    <td> <input type="text" name="codebarang" size="10"> </td>
                </tr>
                <tr>
                    <td> Nama Barang </td>
                    <td> <input type="text" name="namabarang" size="20"> <input type="hidden" name="categoryid" value=" <?php echo"$categoryid" ?> "> </td>
                </tr>
                <tr>
                    <td> Car Vendor Name </td>
                    <td> <select name="carvendorname" id="carvendor"> </select> </td>
                </tr>
                <tr>
                    <td> Car Name </td>
                    <td> <select name="carname" id="car"> </select> </td>
                </tr>
                <tr>
                    <td> Car Year </td>
                    <td> <select name="caryear" id="year"> </select> </td>
                </tr>
                <tr>
                    <td> Category </td>
                    <td> <select name="category" id="category"> </select> </td>
                </tr>
                <tr>
                    <td> Sub-Category </td>
                    <td> <select name="subcategory" id="subcategory"> </select> </td>
                </tr>
                <tr>
                    <td> Supplier </td>
                    <td>
                        <?php
                            include "connect.php";
                            $query3 = "select * from tsupplier";
                            $execute3 = mysql_query($query3, $conn) or die("An error occured. " .mysql_error());
                            echo "<select name=\"supplierid\">";
                            while($row3 = mysql_fetch_array($execute3)){
                                $supplierid = $row3[0];
                                $suppliername = $row3[1];
                                echo "<option value=\"$supplierid\"> $suppliername </option>";
                            }
                            echo "</select>";
                            mysql_close($conn);
                        ?>
                    </td>
                </tr>
                <tr>
                    <td> Quantity </td>
                    <td> <input type="text" name="quantity" size="6"> </td>
                </tr>
                <tr>
                    <td> Satuan </td>
                    <td>
                        <?php
                            include "connect.php";
                            $query4 = "select * from tsatuan";
                            $execute4 = mysql_query($query4, $conn) or die("An error occured. " .mysql_error());
                            echo "<select name=\"satuanid\">";
                            while($row4 = mysql_fetch_array($execute4)){
                                $satuanid = $row4[0];
                                $satuanname = $row4[1];
                                echo "<option value=\"$satuanid\"> $satuanname </option>";
                            }
                            echo "</select>";
                            mysql_close($conn);
                        ?>
                    </td>
                </tr>
                <tr>
                    <td> Code Harga </td>
                    <td> <input type="text" name="codeharga" size="10"> </td>
                </tr>
                <tr>
                    <td> Harga Jual/Satuan </td>
                    <td> <input type="text" name="hargajualsatuan" size="10"> </td>
                </tr>
                <tr>
                    <td> </td>
                    <td> <input type="submit" name="OK" value="OK"> <input type="reset" name="reset" value="Reset"> </td>
                </tr>
            </table>
        </form>
    </body>
    </html>
    'category', 'subcategory', 'car vendor name', 'car name' work fine.. but the 'year' select option doesn't work.. i don't have any idea why this happens.. i traced my code and it goes to updateYear() but it doesn't generate any option..

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by phyra_x99 View Post
    i traced my code and it goes to updateYear() but it doesn't generate any option..
    anything in the Error Console (F12)?


    PS. new mysqli(...) will never return false, even if the connection fails.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    anything in the Error Console (F12)?


    PS. new mysqli(...) will never return false, even if the connection fails.
    no errors found. updateSubCategories() and updateCar() works fine. any suggestion?

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    a live page or the code as received in the browser would help.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    a live page or the code as received in the browser would help.
    i don't understand :|

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    the code problem seems to be in the JS part, so we need to see that part (and the server script does not help).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #7
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    the code problem seems to be in the JS part, so we need to see that part (and the server script does not help).
    the JS part is on that script too, i don't use external script.. i think i have posted everything.

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by phyra_x99 View Post
    i think i have posted everything.
    except for the JS code printed by PHP.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    except for the JS code printed by PHP.
    so what should i do? i don't resolve this yet..

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    open Chrome or Firefox, hit F12 and paste the JS section as shown there in here.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #11
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    open Chrome or Firefox, hit F12 and paste the JS section as shown there in here.
    Code:
    <head>
    	<script type='text/javascript'>
            var categories = [{"id":"1","val":"accessories"},{"id":"2","val":"bungkus jok"},{"id":"3","val":"lampu"}]; 
    var subcategories = {"1":[{"id":"1","val":"outter door"},{"id":"2","val":"garnish lampu"}],"2":[{"id":"3","val":"foglamp"},{"id":"4","val":"led"},{"id":"7","val":"2 baris"},{"id":"8","val":"3 baris"}],"3":[{"id":"5","val":"led"},{"id":"6","val":"foglamp"}]}; 
    var carvendor = [{"id":"1","val":"honda"},{"id":"2","val":"toyota"},{"id":"3","val":"nissan"},{"id":"4","val":"daihatsu"},{"id":"5","val":"kia"},{"id":"6","val":"bmw"},{"id":"7","val":"mercedes benz"},{"id":"8","val":"audi"}]; 
    var car = {"1":[{"id":"1","val":"jazz"},{"id":"2","val":"brio"},{"id":"3","val":"accord"},{"id":"4","val":"cr-z"}],"2":[{"id":"5","val":"kijang"},{"id":"6","val":"innova"},{"id":"7","val":"avanza"},{"id":"8","val":"corolla"},{"id":"9","val":"vios"}],"3":[{"id":"10","val":"juke"},{"id":"11","val":"march"},{"id":"12","val":"serena"}],"4":[{"id":"13","val":"xenia"}],"5":[{"id":"14","val":"carnival"},{"id":"15","val":"carens"},{"id":"16","val":"picanto"},{"id":"17","val":"rio"},{"id":"18","val":"sportage"}],"6":[{"id":"19","val":"320i"},{"id":"20","val":"323i"},{"id":"21","val":"520"},{"id":"22","val":"720"}],"7":[{"id":"23","val":"c200"},{"id":"24","val":"c250"},{"id":"25","val":"c300"},{"id":"26","val":"e200"},{"id":"27","val":"e250"},{"id":"28","val":"e300"}]}; 
    var year = {"":[{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null},{"id":null,"val":null}]}; 
    		
    		window.onload = function(){
    			loadCategories();
    			loadCarvendor();
    		}
    		
            function loadCategories(){
                var categorySelect = document.getElementById("category");
                categorySelect.onchange = updateSubCategories;
                for(var a = 0; a < categories.length; a++){
                    categorySelect.options[a] = new Option(categories[a].val, categories[a].id);          
                }
            }
            function updateSubCategories(){
                var categorySelect = this;
                var categoryid = this.value;
                var subcategorySelect = document.getElementById("subcategory");
                subcategorySelect.options.length = 0;
                for(var a = 0; a < subcategories[categoryid].length; a++){
                    subcategorySelect.options[a] = new Option(subcategories[categoryid][a].val, subcategories[categoryid][a].id);
                }
            }
    		function loadCarvendor(){
    			var carvendorSelect = document.getElementById("carvendor");
    			carvendorSelect.onchange = updateCar;
    			for(var b = 0; b < carvendor.length; b++){
    				carvendorSelect.options[b] = new Option(carvendor[b].val, carvendor[b].id);
    			}
    		}
    		function updateCar(){
    			var carvendorSelect = this;
    			var carvendorid = this.value;
    			var carSelect = document.getElementById("car");
    			carSelect.onchange = updateYear;
    			carSelect.options.length = 0;
    			for(var b = 0; b < car[carvendorid].length; b++){
    				carSelect.options[b] = new Option(car[carvendorid][b].val, car[carvendorid][b].id);
    			}
    		}
    		function updateYear(){
    			var carSelect = this;
    			var carid = this.value;
    			var yearSelect = document.getElementById("year");
    			yearSelect.options.length = 0;
    			for(var b = 0; b < year[carid].length; b++){
    				yearSelect.options[b] = new Option(year[carid][b].val, year[carid][b].id);
    			}
    		}
        </script>
    </head>
    this is what i got. it seems that the php failed to fetch the result of the query.. any ideas?

  • #12
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    at least we know now, what the problem is.

    as for the next step, try out the year query in a direct SQL client (mysql client. phpmyadmin, mysql workbench, etc.) to see, what your query returns.
    Last edited by Dormilich; 07-02-2013 at 07:30 AM.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #13
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    at least we know now, what the problem is.

    as for the next step, try out the year query in a direct SQL client (mysql client. phpmyadmin, mysql workbench, etc.) to see, what your query returns.
    the query works fine.. i have tried it on phpmyadmin..

  • #14
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,350
    Thanks
    13
    Thanked 349 Times in 345 Posts
    if the query is fine, dump the intermediate variables (var_dump($row), var_dump($year))
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #15
    New Coder
    Join Date
    Jun 2013
    Posts
    18
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Dormilich View Post
    if the query is fine, dump the intermediate variables (var_dump($row), var_dump($year))
    where should i assign that code? and what are they for?


  •  
    Page 1 of 2 12 LastLast

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •