简体   繁体   中英

Return Value in Column B Table 2, where column A Table 1, matches Column A Table 2

What I'm trying to do: Where category (projects) matches id (markets) echo category (markets) .

Table 1 Sample ( projects table)

项目表

Table 2 Sample ( markets table)

在此处输入图片说明

Sample of PHP Code

$num_rec_per_page=5;
if (isset($_GET["page"])) { $page  = $_GET["page"]; } else { $page=1; }; 
$start_from = ($page-1) * $num_rec_per_page; 

 function GET($key) {
    return isset($_GET[$key]) ? $_GET[$key] : null;
}

$category= GET('category');

if ($category !== null) {
    $sql = "SELECT * FROM projects WHERE category='$category' LIMIT $start_from, $num_rec_per_page";
} else {
    $sql = "SELECT * FROM projects LIMIT $start_from, $num_rec_per_page";
}

$sql_query = mysql_query($sql);
$post = $sql_query;

$sql1 = "SELECT * FROM markets";
$sql_query1 = mysql_query($sql1);
$marketsinfo = mysql_fetch_array($sql_query1);

In my code below, I've tried putting a while loop within the main while loop since we have to find out what category its in then display it for each blog post.

It only worked for the first result, and then I did some research online and found that it is very poor design to do this.

Where I'm currently at with displaying the result (see code in between hyphens):

            <!-- Blog - Start -->
        <?php while ($post = mysql_fetch_array($sql_query)) {?>
        <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 blog blog_altered blog_left">
            <div class="row">
                <!-- Blog Image - Start -->
                <div class=" col-lg-6 col-md-6 col-sm-10 col-xs-12  pic inviewport animated delay1" data-effect="fadeIn">
                    <img alt="blog-image" class="img-responsive" src="<?php echo $post['imageoutside'] ?>">
                </div>
                <!-- Blog Image - End -->
                <!-- Blog Info - Start -->
                <div class="col-lg-6 col-md-6 col-sm-12 col-xs-12 inviewport animated delay1" data-effect="fadeIn">
                    <div class="info">
----------------------------------------------------------------------
                        <span class="date">
                        <?php
                            if($post['category'] === $marketsinfo['id']){
                                echo $marketsinfo['category'];
                            }
                         ?>
                         </span>
----------------------------------------------------------------------
                        <h4 class="title"><a href="projects/readmore.php?job=<?php echo $post['job'] ?>"><?php echo $post['title'] ?></a></h4>
                        <p><?php echo $post['summary'] ?></p>
                        <a class="btn btn-primary text-on-primary"  href="projects/readmore.php?job=<?php echo $post['job'] ?>">Read More</a>
                    </div>
                </div>
                <!-- Blog Info - End -->
            </div>
        </div>
        <?php } ?>
            <!-- Blog - End -->

Hope I've been thorough enough without being confusing. How can a noob accomplish this? I'll take any pointers you have!!

If you use a join, you can get the category text in one query and avoid all the looping in PHP.

A LEFT Join will return all records from Projects and only those records which match from markets. So if a projects category doesn't exist in markets, a NULL value will be returned.

If you used a INNER JOIN, only records which have corresponding values in BOTH tables would be returned.

Rule of thumb: get the data you need from the database in 1 trip when you need it. Format in PHP, Datagrab in SQL. Don't get more than you need, and don't get less.

SELECT P.Job, M.ID as Markets_ID, M.Category, P.Title
FROM projects P
LEFT JOIN Markets M
 on P.Category =M.ID 
WHERE P.category='$category'   
LIMIT $start_from, $num_rec_per_page"

Note: you will need to put a table alias on category in the where clause. I'm assuming your passing in the ID so P.Category was used.

do you want join table projects with tables market by category? may be u can do this

SELECT p.id as id
     , m.category as category 
  from projects as p 
  left 
  join markets as m 
    on p.category = m.id 
 WHERE m.category='$category' 
 LIMIT $start_from
     , $num_rec_per_page

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM