Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

PHP Question about setting another variable

chrisj

Bronze Coder
The web video script that I'm working on modifying currently allows for submitting videos, via upload Form.
Among other fields, the upload Form allows the uploader to select a subcategory.

I believe the code below is querying the database to get the COUNT where the 'sub_category_id' exists.
If it is found then it sets another variable, $sub_category to the value of that ID.
(The subcategory id's are stored in the 'LANGS' table > 'lang_key' column).

Does what I believe this code is doing sound correct?

PHP:
$sub_category = 0;
 if (!empty($_POST['sub_category_id'])) {
     $is_found = $db->where('type',PT_Secure($_POST['category_id']))->where('lang_key',PT_Secure($_POST['sub_category_id']))->getValue(T_LANGS,'COUNT(*)');
                if ($is_found > 0) {
                    $sub_category = PT_Secure($_POST['sub_category_id']);
                }
            }

My next question is, regarding "sets another variable, $sub_category to the value of that ID"

how can I set another variable to the value of the ID's corresponding sub category 'name'
(which is also stored in the 'langs' table > 'names' column) ?

I look forward to any assistance.
 
The code you posted above is taking the value of sub_category_id and is querying your database table to see if any rows come back where that sub_category_id as well as the category_id also submitted by the form match. This is done by getting a count of the number of rows returned, anything more than zero and at least one match was found. You are correct that the variable gets set with the value of the sub_category_id if a match is found.

To get the value of the name column, you would have to alter your query to get that either instead of or in addition to the count. Unfortunately I don't see a table name listed in your database query code, so I am assuming that gets set earlier in the script. Also, which database class / library are you using?
 
Thanks for your reply.
Unfortunately, I don't understand what you mean by "to get the value of the name column, you would have to alter your query to get that either instead of or in addition to the count". could you provide an example?

Regarding, database, the script shows: MySQLi

I lookf forward to any assistance
 
I'm thinking you would want to do something like:

PHP:
$name = $db->where('type',PT_Secure($_POST['category_id']))->where('lang_key',PT_Secure($_POST['sub_category_id']))->getValue(T_LANGS,'name');

However, I am not sure if that is correct given how you are querying the database since your code doesn't appear to include the part where you specify the table name you're querying. I also don't know what methods are in the $db class you're using so I can't really give further guidance, since it appears you're using a library to build the query for you, instead of writing the SQL yourself.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom