CCates
CCates

Reputation: 1255

A second try at restricting searches in mySQL & PHP

So, I am really balls confused here. I have no idea what I'm doing, but, I'm trying to restrict searches based on networks being either private or public.

This code does not work, but, I'm trying to figure out how to make this work. How do I make private networks only appear to people in that network????????????????

    $query = mysql_query("SELECT * FROM requests ORDER BY dateposted DESC");

while ($row = mysql_fetch_assoc($query)){

    $network = mysql_query("SELECT * FROM network");

    while ($row2 = mysql_fetch_assoc($network)) {
        $display = false;
        $pos = strpos($row['network'], $search);
        if ($row2['visible'] == "public" and $search == "Global") {
            $display = true;
        } else          
        if ($pos == true and 
        ($row2['visible'] == "public" or $row2['username'] == $username)) {
            $display = true;
        } else
        if  ($row2['visible'] == "private") {
            $display = false;
        }
    }
        echo $display;
        if ($display == true){
            echo "<div id='".$row["id"]."' class='item'>";
            echo '<div style="width: 75%;">';
            echo "<h4 style='display: inline;'>".$row["user"]." requests:</h4><br/>";
            echo "<h2 style='display: inline;'>".$row["title"]."  </h2>";
            echo '</div>';

            echo '<h3 id="button" style="border-radius: 10px; padding: 4px; color: white; float: right; position: relative; top: -50px; border: 2px solid grey; background-color: #C0504D;">Apply</h2>';
            echo "</div>";
        }
}

The network schema:

network text    utf8_general_ci     No                                   
username    text    utf8_general_ci     No                                   
visible text    utf8_general_ci     No  

The requests schema:

id  int(11)         No      auto_increment                          
user    text    utf8_general_ci     No                                   
title   text    utf8_general_ci     No                                   
description text    utf8_general_ci     No                                   
picture text    utf8_general_ci     No                                   
price   text    utf8_general_ci     No                                   
premium datetime            No                                  
type    text    utf8_general_ci     No                                   
network text    utf8_general_ci     No                                   
dateposted  datetime            No  

Upvotes: 1

Views: 101

Answers (1)

user800014
user800014

Reputation:

It's a little bit late here, so I will try to better write this answer.

You need to print all public networks, and only private networks related to the user. Also, if I understood, your search can be "Global" or have a part name of a network, correct?

So your query can be something like this:

if( $search == "Global" ) {
  $sql = "select n.*
               , r.* 
            from network n
           inner join request r on n.network = r.network
           where  n.network = 'public' 
              or (n.network = 'private' 
             and  n.username = '".$username."')";
} else {
  $sql = "select n.*
               , r.* 
              from network n
             inner join request r on n.network = r.network
             where  n.network = 'public' 
                or (n.network = 'private' 
               and  n.username = '".$username."')
               and n.network like '%".mysql_real_escape_string($search)."%'";
}

The like here means "select me all data where the network name contains the content of search. You can see more examples and explanations here and in the mysql docs.

Upvotes: 1

Related Questions