2

I am hoping I am using the correct language for this. I am decent at coding, but I am struggling to figure this one out.

Basically, I have a database of categories and templates. I want to make it so my small code, produces on its own, based on the categories which then pulls from the templates. I am stuck at the string making portion. When pulling the categories from the database for the tabs, I need it to also make the MySQL query string, based on some information from the categories table. The rest I can figure out, I just need to get this working. The columns in the categories table are id number shortname tabname catname. Please see the current full code below, with the exception of the actual tabs content:

 <?php
    $connect = mysqli_connect("localhost", "brandina_templat", "PWREMOVED", "brandina_templates");  
    $output = '';  
    $catresult = "SELECT * FROM categories";
    $catquery = mysqli_query($connect,$catresult);
    echo "
    <html>  
          <head>  
               <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />  
               <title>Templates Sheet | Brandin Arsenault's</title>    
               <script src='js/bootstrap.js'></script>  
               <script src='js/tabcontent.js'></script> 
               <link href='css/bootstrap.css' rel='stylesheet' />  
          </head>   
          <body>  
           <div class='container'>  
                    <br />  
                    <h1 align='center'>Templates Sheet</h1>
                        <center><ul class='tabs'>";
        if(!$catquery)
        {
            die('Invalid query: ' . mysql_error());
        }
            while($row = mysqli_fetch_array($catquery))
            {
                $id=$row['id'];
                $tabname=$row['tabname'];
                $catname=$row['catname'];
                echo "<li class='tab-link current' data-tab='$tabname'>$catname</li>";
            }
        echo "</ul></center>";
        INSERT QUERY HERE FOR CATEGORY, e.g. $($id)query = SELECT blah blah
    echo "
    </body>
    </html>";
    ?>

Thanks in advance.

Updated Code 9/6/16 at 4PM AST:

if(!$catquery)
{
    die('Invalid query: ' . mysql_error());
}
    while($row = mysqli_fetch_array($catquery))
    {
        $id=$row['id'];
        $number=$row['number'];
        $tabname=$row['tabname'];
        $catname=$row['catname'];
        echo "<li class='tab-link current' data-tab='$tabname'>$catname</li>";
        $result[$id] = "SELECT * FROM templates WHERE category=$number";
        $query[$id] = mysqli_query($connect,$result[$id]);
            echo "$query[$id]";
    }

1 Answer 1

1

First things first, learn how to use PDO's: their very useful in the long run.

Secondly this should be avoided

<?php
funcs_nstuff();
echo("<html> ...
      ...             
      </html>");
?>

Do this instead:

<?php
funcs_nstuff();
?>
<html> ...
...
</html>

Thirdly: I'm confused with you're question greatly. The english is awful. Or perhaps it's my dumbassary. Always avoid copying and pasting your entire project into stack overflow, no one wants to read it. Post a simple bit of code that shows the part where you're struggling.

REGARDLESS, I will answer the best I can... The way I interpret it is you have Query A that get's some rows... then using those rows you need to tailor Query B. And now you're confused about how to tailor them. Let let me start off by saying you're thinking too hard:

$queryB = "SELECT * FROM Templates WHERE Templates.Category=$catname"

that statement is thus tailored to be in the context of Query A.

Sign up to request clarification or add additional context in comments.

5 Comments

Thank you for your response and I am sorry for the issues my questioncreated. The reason I included all of the code is per a response on another question of mine regarding a totally difference script. But, you did answer part of my question. Now, where it says queryB is what I am trying to have set automatically. I want $queryB to be duplicated for every category, and I know I can't use the same $ for each query. So, I want the string to be something like $($catid)query or something like that. I hope that makes sense.
Ah, I see. Well one solution is to use an array or queries would seem to be the best fit to what you're trying to do. Ie $queryB[$catid] = "SELECT ..."
@BrandinArsenault Or if you really want to be tricky (and rather inefficient) you could do it like: $catid = 4; $name = "query" . $catid; $$name = "query"; echo $query4;
thanks for your help. I did as you suggested, and am using echo "$query['$id"; to try and ensure that it is actually grabbing a result, and it appears if it is not. See my current code in the main post.
$query[$id] = mysqli_query($connect,$result[$id]); echo "$query[$id]"; = you're trying to echo out a result object. You still need to use $row = mysqli_fetch_array($query[$id]).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.