Jump to content

Recommended Posts

Hi everyone,
When I choose one of the value, I want this shown in the url. example: index.php?espess_segmento=450 . But display the values of "diametro" available in the table where the espess_segmento=450. After this I want can choose the values of "diametro" and appear in url something like this: index.php?espess_segmento=450&diametro=3.

This the code I write:
 

<?php
    function getCats1 ()
    {
    global $con;
    $get_cats = "select * from categorias";
    $run_cats = mysqli_query ($con, $get_cats);

    while($row_cats= mysqli_fetch_array ($run_cats))
    {
        $cat_id = $row_cats['cat_id'];
        $cat_title = $row_cats['cat_title'];
    }

    if(isset($_GET['cat'])){

        $cat= $_GET['cat'];
        global $con;
    //Diamentro   
        $get_cat_pro = "SELECT * FROM produtos where cat='$cat' ";
        $run_cat_pro = mysqli_query ($con, $get_cat_pro);
        echo"Diâmetro <select name='diametro' onchange='location= this.value;'>";
        while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
                $pro_diametro = $row_cat_pro['diametro'] ;
                $link_diametro= "index.php?diametro=$pro_diametro";

                echo "<input type='radio' value='" .$link_diametro ."' >" . $pro_diametro . "</option>";
                      }
        echo "</br>";

    //Espess_segmento   
        $get_cat_pro2 = "SELECT * FROM produtos where cat='$cat' ";
        $run_cat_pro2 = mysqli_query ($con, $get_cat_pro2);
        echo"Espessura de Segmento <select name='espess_segmento' onchange='location= this.value;'>";
        while($row_cat_pro2=mysqli_fetch_array($run_cat_pro2)){
                $pro_espess_segmento = $row_cat_pro2['espess_segmento'] ;
                $link_espess_segmento= "index.php?espess_segmento=$pro_diametro";

                echo "<option value='" .$link_espess_segmento ."' >" . $pro_espess_segmento . "</option>";
                      }
        echo "</select>";

    }   
    }
    ?>

What I need do?
Thanks for the attention!

Link to comment
https://forums.phpfreaks.com/topic/301155-urls-and-database/
Share on other sites

did you write this code or copy it from somewhere? So many mistakes.

 

Try fixing the function at the top. You loop thru the query results in there, saving all the rows values in the same variable. This means you only have the last row's information to work with. Why is that?

 

In the main line code you are doing this:

while($row_cat_pro=mysqli_fetch_array($run_cat_pro))
{
$pro_diametro = $row_cat_pro['diametro'];
$link_diametro= "index.php?diametro=$pro_diametro";
echo "<input type='radio' value='" .$link_diametro ."' >" . $pro_diametro . "</option>";
}

Why do you echo the start of an input tag and end with a closing option tag??? You do this again later. Do you not know HTML at all?

Link to comment
https://forums.phpfreaks.com/topic/301155-urls-and-database/#findComment-1532820
Share on other sites

You may want to look into a routing system where this is done for you. You can spin your own using PHP and .htaccess, but there are several available online that can take care of it for you and have the benefit of being widely tested and publicly developed. The biggest thing for this is it's easier for your users to remember, it's more search engine friendly, you don't have to write and test it, and you know exactly what you are getting and where it's coming from.

 

In addition to the things @ginerjm pointed out, I'd highly recommend switching from mysli to PDO. Prepared statements are much easier to deal with in PDO, and you should never, ever inject user-submitted data (such as $_POST variables, let alone $_GET) into a database call. Seriously, don't do it.

 

You're also declaring $con as a global variable. Although there's nothing technically wrong with doing this, it's not a good idea. Things get very confusing very quickly when you declare a variable to be global; where is that variable declared? What was in it when it was declared? What was put into it later, either by accident or intention? Passing the variable to the function leaves a clear trail of accountability that you can trace when things - as they inevitably do - go south.

 

I also just noticed that you seem to be running the exact same query twice. Why?

Edited by maxxd
Link to comment
https://forums.phpfreaks.com/topic/301155-urls-and-database/#findComment-1532849
Share on other sites

I declared $con in the begin of the index from other page (include etc etc).


 

You're also declaring $con as a global variable.

$con = mysqli_connect ("localhost","root","xxxx","xxxxx");



I don't need do this?
 

 

    //Diamentro   
        $get_cat_pro = "SELECT * FROM produtos where cat='$cat' ";
        $run_cat_pro = mysqli_query ($con, $get_cat_pro);
        echo"Diâmetro <select name='diametro' onchange='location= this.value;'>";
        while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
                $pro_diametro = $row_cat_pro['diametro'] ;
                $link_diametro= "index.php?diametro=$pro_diametro";

                echo "<input type='radio' value='" .$link_diametro ."' >" . $pro_diametro . "</option>";
                      }
        echo "</br>";

    //Espess_segmento   
        $get_cat_pro2 = "SELECT * FROM produtos where cat='$cat' ";
        $run_cat_pro2 = mysqli_query ($con, $get_cat_pro2);
        echo"Espessura de Segmento <select name='espess_segmento' onchange='location= this.value;'>";
        while($row_cat_pro2=mysqli_fetch_array($run_cat_pro2)){
                $pro_espess_segmento = $row_cat_pro2['espess_segmento'] ;
                $link_espess_segmento= "index.php?espess_segmento=$pro_diametro";

                echo "<option value='" .$link_espess_segmento ."' >" . $pro_espess_segmento . "</option>";
                      }
        echo "</select>";

I can do something like this?
 

$get_cat_pro = "SELECT * FROM produtos where cat='$cat' ";
        $run_cat_pro = mysqli_query ($con, $get_cat_pro);
        echo"Diâmetro <select name='diametro' onchange='location= this.value;'>";
        while($row_cat_pro=mysqli_fetch_array($run_cat_pro)){
                $pro_diametro = $row_cat_pro['diametro'] ;
                $link_diametro= "index.php?diametro=$pro_diametro";
                $pro_espess_segmento = $row_cat_pro['espess_segmento'] ;
                $link_espess_segmento= "index.php?espess_segmento=$pro_diametro";

                echo "<input type='radio' value='" .$link_diametro ."' >" . $pro_diametro . "</option>";
                      }
        echo "</br>";

                echo "<option value='" .$link_espess_segmento ."' >" . $pro_espess_segmento . "</option>";
                      }
        echo "</select>";

I will read more about PDO

Link to comment
https://forums.phpfreaks.com/topic/301155-urls-and-database/#findComment-1532854
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.