2011-08-15 5 views
0

2 개의 쿼리가 있습니다. 첫 번째 쿼리는 제품 제목을 가져오고 두 번째 쿼리는 제품 이미지를 가져와 그리드 레이아웃을 만듭니다. 두 쿼리 모두 1 개의 테이블에서 조회를 수행합니다.단일 테이블의 쿼리 결합

$sql = "SELECT post_title FROM `wordpress`.`wp_posts` ". 
      "WHERE post_type ='wpsc-product' ". 
      "AND post_status = 'publish' ORDER BY `wp_posts`.`ID`"; 
$result = mysql_query($sql) or die(mysql_error()); 
while($row = mysql_fetch_array($result)){ 

    $title = $row['post_title']; 
} 

$sql2 = "SELECT guid FROM `wordpress`.`wp_posts` ". 
     "WHERE post_mime_type ='image/png' ORDER BY `wp_posts`.`ID`"; 
echo "<table border=\"10\" style=\"margin-top:10px;\">"; 
echo "<tr>"; 
$i=1; 
$result2 = mysql_query($sql2) or die(mysql_error()); 


while($row2 = mysql_fetch_array($result2)){ 

    $image = $row2['guid']; 
    echo "<td><a href=".$image.">". 
     "<IMG style=\"width:100px;height:100px;". 
         "vertical-align:top; padding-right: 10px; " . 
         (($i %2 == "0") ? "padding-left: 30px;" : "") . 
     " \" SRC=\"$image\" ALT=\"align box\" ALIGN=LEFT></a>"; 

    echo "<a style=\"text-decoration:underline;\" ". 
      "onmouseover=\"this.style.textDecoration = 'none'\" ". 
      "onmouseout=\"this.style.textDecoration = 'underline'\"". 
      "href=".$lol.">$title</a><br>"; 
    echo "<p style=\"color:#990000; padding-top:5px; font-weight:bold;\">". 
      " $19.90</p> <a style=\"font-weight:lighter;\" href=".$lol.">". 
      "More info<img style=\"padding-left:3px;\" ". 
      "src=\"http://localhost/famfamfam.png\"></a></td>"; 
    if ($i++ % 2 == 0) { 
     echo "</tr><tr><td style=\"padding-right:20px;\" colspan=\"1\">". 
      "<hr style=\"width:250px;\" /></td> ". 
      "<td style=\"padding-right: 20px; padding-left: 30px;\" ". 
      "colspan=\"1\"><hr style=\"width:250px;\" /></td></tr>"; 
    } 
} 
echo "</tr></table>"; 

나는 첫 번째 쿼리 ($ 제목)의 결과를 사용하고 루프 쿼리 ($ 제목) 동안 초를 사용하고 싶습니다.

어떻게하면됩니까? 미리 감사드립니다.




UPDATE

@Muhammad 시르 당신의 @cwallenpoole 모두 작동하지 않았다 대답 할하지만 단일 쿼리를 사용하기 때문에 내가 @cwallenpoole 아이디어를 선호합니다. 두 사람의 이미지 출력은 다음과 같습니다. 그것은 동일합니다 : http://i.imgur.com/pf01M.png

저는 실제로 php로 결과를 필터링 할 수 있지만 대신 SQL을 사용하여 쿼리 결과를 구체화 할 수 있기를 기대했습니다.

$sql2 = "SELECT * FROM `wordpress`.`wp_posts`"; 
echo "<table border=\"10\" style=\"margin-top:10px;\">"; 
echo "<tr>"; 
$i=1; 
$result2 = mysql_query($sql2) or die(mysql_error()); 


while($row2 = mysql_fetch_array($result2)){ 

if ($row2['post_status'] == 'publish' && $row2['post_type'] == 'wpsc-product'){ 
     $title = $row2['post_title']; 
} 

//echo $row2['guid']; 

if ($row2['post_mime_type'] == 'image/png') 
{ 
$image = $row2['guid']; 



      echo "<td><a href=".$image."><IMG style=\"width:100px;height:100px;vertical-align:top; padding-right: 10px; " . (($i %2 == "0") ? "padding-left: 30px;" : "") . " \" SRC=\"$image \" ALT=\"align box\" ALIGN=LEFT></a>"; 

    echo "<a style=\"text-decoration:underline;\" onmouseover=\"this.style.textDecoration = 'none'\" onmouseout=\"this.style.textDecoration = 'underline'\" href=".$lol."> $title </a><br>"; 
    echo "<p style=\"color:#990000; padding-top:5px; font-weight:bold;\"> $19.90</p> <a style=\"font-weight:lighter;\" href=".$lol.">More info<img style=\"padding-left:3px;\" src=\"http://localhost/famfamfam.png\"></a> 
</td>"; 
    if ($i++ % 2 == 0) { 
     echo "</tr><tr><td style=\"padding-right:20px;\" colspan=\"1\"><hr style=\"width:250px;\" /></td> <td style=\"padding-right: 20px; padding-left: 30px;\" colspan=\"1\"><hr style=\"width:250px;\" /></td></tr>"; 
    } 
} 




} 
echo "</tr></table>"; 



내가 원하는 출력 : 여기에 http://i.imgur.com/Vjq2Q.png

는 SQL 파일입니다. 누군가 내 코드를 최적화하는 데 도움이되기를 바랍니다.

-- phpMyAdmin SQL Dump 
-- version 3.2.4 
-- http://www.phpmyadmin.net 
-- 
-- Host: localhost 
-- Generation Time: Aug 16, 2011 at 06:07 AM 
-- Server version: 5.1.41 
-- PHP Version: 5.3.1 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO"; 

-- 
-- Database: `wordpress` 
-- 

-- -------------------------------------------------------- 

-- 
-- Table structure for table `wp_posts` 
-- 

CREATE TABLE IF NOT EXISTS `wp_posts` (
    `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT, 
    `post_author` bigint(20) unsigned NOT NULL DEFAULT '0', 
    `post_date` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_date_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_content` longtext NOT NULL, 
    `post_title` text NOT NULL, 
    `post_excerpt` text NOT NULL, 
    `post_status` varchar(20) NOT NULL DEFAULT 'publish', 
    `comment_status` varchar(20) NOT NULL DEFAULT 'open', 
    `ping_status` varchar(20) NOT NULL DEFAULT 'open', 
    `post_password` varchar(20) NOT NULL DEFAULT '', 
    `post_name` varchar(200) NOT NULL DEFAULT '', 
    `to_ping` text NOT NULL, 
    `pinged` text NOT NULL, 
    `post_modified` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_modified_gmt` datetime NOT NULL DEFAULT '0000-00-00 00:00:00', 
    `post_content_filtered` text NOT NULL, 
    `post_parent` bigint(20) unsigned NOT NULL DEFAULT '0', 
    `guid` varchar(255) NOT NULL DEFAULT '', 
    `menu_order` int(11) NOT NULL DEFAULT '0', 
    `post_type` varchar(20) NOT NULL DEFAULT 'post', 
    `post_mime_type` varchar(100) NOT NULL DEFAULT '', 
    `comment_count` bigint(20) NOT NULL DEFAULT '0', 
    PRIMARY KEY (`ID`), 
    KEY `post_name` (`post_name`), 
    KEY `type_status_date` (`post_type`,`post_status`,`post_date`,`ID`), 
    KEY `post_parent` (`post_parent`), 
    KEY `post_author` (`post_author`) 
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=28 ; 

-- 
-- Dumping data for table `wp_posts` 
-- 

INSERT INTO `wp_posts` (`ID`, `post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_excerpt`, `post_status`, `comment_status`, `ping_status`, `post_password`, `post_name`, `to_ping`, `pinged`, `post_modified`, `post_modified_gmt`, `post_content_filtered`, `post_parent`, `guid`, `menu_order`, `post_type`, `post_mime_type`, `comment_count`) VALUES 
(1, 1, '2011-08-16 01:32:00', '2011-08-16 01:32:00', 'Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!', 'Hello world!', '', 'publish', 'open', 'open', '', 'hello-world', '', '', '2011-08-16 01:32:00', '2011-08-16 01:32:00', '', 0, 'http://localhost/?p=1', 0, 'post', '', 1), 
(2, 1, '2011-08-16 01:32:00', '2011-08-16 01:32:00', 'This is an example page. It''s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:\n\n<blockquote>Hi there! I''m a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like pi&#241;a coladas. (And gettin'' caught in the rain.)</blockquote>\n\n...or something like this:\n\n<blockquote>The XYZ Doohickey Company was founded in 1971, and has been providing quality doohickies to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.</blockquote>\n\nAs a new WordPress user, you should go to <a href="http://localhost/wp-admin/">your dashboard</a> to delete this page and create new pages for your content. Have fun!', 'Sample Page', '', 'publish', 'open', 'open', '', 'sample-page', '', '', '2011-08-16 01:32:00', '2011-08-16 01:32:00', '', 0, 'http://localhost/?page_id=2', 0, 'page', '', 0), 
(3, 1, '2011-08-16 01:37:18', '0000-00-00 00:00:00', '', 'Auto Draft', '', 'auto-draft', 'open', 'open', '', '', '', '', '2011-08-16 01:37:18', '0000-00-00 00:00:00', '', 0, 'http://localhost/?p=3', 0, 'post', '', 0), 
(4, 1, '2011-08-16 01:38:28', '2011-08-16 01:38:28', '[productspage]', 'Products Page', '', 'publish', 'closed', 'closed', '', 'products-page', '', '', '2011-08-16 01:38:28', '2011-08-16 01:38:28', '', 0, 'http://localhost/?page_id=4', 0, 'page', '', 0), 
(5, 1, '2011-08-16 01:38:28', '2011-08-16 01:38:28', '[shoppingcart]', 'Checkout', '', 'publish', 'closed', 'closed', '', 'checkout', '', '', '2011-08-16 01:38:28', '2011-08-16 01:38:28', '', 4, 'http://localhost/?page_id=5', 0, 'page', '', 0), 
(6, 1, '2011-08-16 01:38:28', '2011-08-16 01:38:28', '[transactionresults]', 'Transaction Results', '', 'publish', 'closed', 'closed', '', 'transaction-results', '', '', '2011-08-16 01:38:28', '2011-08-16 01:38:28', '', 4, 'http://localhost/?page_id=6', 0, 'page', '', 0), 
(7, 1, '2011-08-16 01:38:28', '2011-08-16 01:38:28', '[userlog]', 'Your Account', '', 'publish', 'closed', 'closed', '', 'your-account', '', '', '2011-08-16 01:38:28', '2011-08-16 01:38:28', '', 4, 'http://localhost/?page_id=7', 0, 'page', '', 0), 
(8, 1, '2011-08-16 01:42:13', '2011-08-16 01:42:13', 'this\r\n\r\nis\r\n\r\na\r\n\r\ntst', 'Kaspersky Internet Security', '', 'publish', 'open', 'closed', '', 'kaspersky-internet-security', '', '', '2011-08-16 02:29:33', '2011-08-16 02:29:33', '', 0, 'http://localhost/?post_type=wpsc-product&#038;p=8', 0, 'wpsc-product', '', 0), 
(9, 1, '2011-08-16 01:41:49', '2011-08-16 01:41:49', '', '5580', '', 'inherit', 'open', 'open', '', '5580', '', '', '2011-08-16 01:41:49', '2011-08-16 01:41:49', '', 8, 'http://localhost/wp-content/uploads/2011/08/55803.png', 0, 'attachment', 'image/png', 0), 
(10, 1, '2011-08-16 01:45:38', '2011-08-16 01:45:38', 'this\r\n\r\nis\r\n\r\na\r\n\r\ntest', 'Logitech M508', '', 'publish', 'open', 'closed', '', 'logitech-m508', '', '', '2011-08-16 02:35:18', '2011-08-16 02:35:18', '', 0, 'http://localhost/?post_type=wpsc-product&#038;p=10', 0, 'wpsc-product', '', 0), 
(11, 1, '2011-08-16 01:45:18', '2011-08-16 01:45:18', '', 'wormmm', '', 'inherit', 'open', 'open', '', 'wormmm', '', '', '2011-08-16 01:45:18', '2011-08-16 01:45:18', '', 10, 'http://localhost/wp-content/uploads/2011/08/wormmm2.png', 0, 'attachment', 'image/png', 0), 
(12, 1, '2011-08-16 01:48:16', '2011-08-16 01:48:16', 'this\r\n\r\nis\r\n\r\na\r\n\r\ntest', 'StG 44', '', 'publish', 'closed', 'closed', '', 'stg-44', '', '', '2011-08-16 01:48:16', '2011-08-16 01:48:16', '', 0, 'http://localhost/?post_type=wpsc-product&#038;p=12', 0, 'wpsc-product', '', 0), 
(13, 1, '2011-08-16 01:48:00', '2011-08-16 01:48:00', '', 'port', '', 'inherit', 'open', 'open', '', 'port', '', '', '2011-08-16 01:48:00', '2011-08-16 01:48:00', '', 12, 'http://localhost/wp-content/uploads/2011/08/port.png', 0, 'attachment', 'image/png', 0), 
(14, 1, '2011-08-16 01:49:41', '2011-08-16 01:49:41', 'this\r\n\r\nis\r\n\r\na\r\n\r\ntst', 'Bullet train', '', 'publish', 'closed', 'closed', '', 'bullet-train', '', '', '2011-08-16 01:49:41', '2011-08-16 01:49:41', '', 0, 'http://localhost/?post_type=wpsc-product&#038;p=14', 0, 'wpsc-product', '', 0), 
(15, 1, '2011-08-16 01:49:24', '2011-08-16 01:49:24', '', 'KIS', '', 'inherit', 'open', 'open', '', 'kis', '', '', '2011-08-16 01:49:24', '2011-08-16 01:49:24', '', 14, 'http://localhost/wp-content/uploads/2011/08/KIS.png', 0, 'attachment', 'image/png', 0), 
(16, 1, '2011-08-16 01:50:39', '2011-08-16 01:50:39', 'this \r\n\r\nis\r\n\r\na\r\n\r\ntest', 'Stack Over Flow', '', 'publish', 'open', 'closed', '', 'stack-over-flow', '', '', '2011-08-16 01:50:39', '2011-08-16 01:50:39', '', 0, 'http://localhost/?post_type=wpsc-product&#038;p=16', 0, 'wpsc-product', '', 0), 
(18, 1, '2011-08-16 01:57:26', '2011-08-16 01:57:26', '', 'cat', '', 'inherit', 'open', 'open', '', 'cat', '', '', '2011-08-16 01:57:26', '2011-08-16 01:57:26', '', 16, 'http://localhost/wp-content/uploads/2011/08/cat.png', 0, 'attachment', 'image/png', 0), 
(19, 1, '2011-08-16 02:26:40', '2011-08-16 02:26:40', '[productspage]', '', '', 'publish', 'open', 'open', '', '19', '', '', '2011-08-16 02:27:36', '2011-08-16 02:27:36', '', 0, 'http://localhost/?p=19', 2, 'nav_menu_item', '', 0), 
(20, 1, '2011-08-16 02:26:40', '2011-08-16 02:26:40', '[userlog]', '', '', 'publish', 'open', 'open', '', '20', '', '', '2011-08-16 02:27:36', '2011-08-16 02:27:36', '', 4, 'http://localhost/?p=20', 3, 'nav_menu_item', '', 0), 
(21, 1, '2011-08-16 02:26:40', '2011-08-16 02:26:40', '[transactionresults]', '', '', 'publish', 'open', 'open', '', '21', '', '', '2011-08-16 02:27:36', '2011-08-16 02:27:36', '', 4, 'http://localhost/?p=21', 5, 'nav_menu_item', '', 0), 
(22, 1, '2011-08-16 02:26:40', '2011-08-16 02:26:40', '[shoppingcart]', '', '', 'publish', 'open', 'open', '', '22', '', '', '2011-08-16 02:27:36', '2011-08-16 02:27:36', '', 4, 'http://localhost/?p=22', 4, 'nav_menu_item', '', 0), 
(23, 1, '2011-08-16 02:26:40', '2011-08-16 02:26:40', 'This is an example page. It’s different from a blog post because it will stay in one place and will show up in your site navigation (in most themes). Most people start with an About page that introduces them to potential site visitors. It might say something like this:\r\n\r\nHi there! I’m a bike messenger by day, aspiring actor by night, and this is my blog. I live in Los Angeles, have a great dog named Jack, and I like piña coladas. (And gettin’ caught in the rain.)\r\n\r\n…or something like this:\r\n\r\nThe XYZ Doohickey Company was founded in 1971, and has been providing quality doohickies to the public ever since. Located in Gotham City, XYZ employs over 2,000 people and does all kinds of awesome things for the Gotham community.\r\n\r\nAs a new WordPress user, you should go to your dashboard to delete this page and create new pages for your content. Have fun!', '', '', 'publish', 'open', 'open', '', '23', '', '', '2011-08-16 02:27:35', '2011-08-16 02:27:35', '', 0, 'http://localhost/?p=23', 1, 'nav_menu_item', '', 0), 
(24, 1, '2011-08-16 02:28:11', '2011-08-16 02:28:11', 'null', 'Stack Over Flow (1 PC (1 Year))', '', 'inherit', 'open', 'open', '', 'stack-over-flow-1-pc-1-year', '', '', '2011-08-16 02:28:11', '2011-08-16 02:28:11', '', 16, 'http://localhost/?wpsc-product=stack-over-flow-1-pc-1-year', 0, 'wpsc-product', '', 0), 
(25, 1, '2011-08-16 02:28:11', '2011-08-16 02:28:11', 'null', 'Stack Over Flow (3 PC (1 Year))', '', 'inherit', 'open', 'open', '', 'stack-over-flow-3-pc-1-year', '', '', '2011-08-16 02:28:11', '2011-08-16 02:28:11', '', 16, 'http://localhost/?wpsc-product=stack-over-flow-3-pc-1-year', 0, 'wpsc-product', '', 0), 
(26, 1, '2011-08-16 02:28:55', '2011-08-16 02:28:55', 'null', 'Kaspersky Internet Security (1 PC (1 Year))', '', 'inherit', 'open', 'open', '', 'kaspersky-internet-security-1-pc-1-year', '', '', '2011-08-16 02:28:55', '2011-08-16 02:28:55', '', 8, 'http://localhost/?wpsc-product=kaspersky-internet-security-1-pc-1-year', 0, 'wpsc-product', '', 0), 
(27, 1, '2011-08-16 02:28:55', '2011-08-16 02:28:55', 'null', 'Kaspersky Internet Security (3 PC (1 Year))', '', 'inherit', 'open', 'open', '', 'kaspersky-internet-security-3-pc-1-year', '', '', '2011-08-16 02:28:55', '2011-08-16 02:28:55', '', 8, 'http://localhost/?wpsc-product=kaspersky-internet-security-3-pc-1-year', 0, 'wpsc-product', '', 0); 

감사합니다 :)

답변

0

$j 같은 카운터 변수를 사용하여

$j = 0;  
while($row2 = mysql_fetch_array($result2)){ 
    $t = $title[$j]; // this will be your title 
    $image = $row2['guid']; 
    echo "<td><a href=".$image."><IMG style=\"width:100px;height:100px;vertical-align:top; padding-right: 10px; " . (($i %2 == "0") ? "padding-left: 30px;" : "") . " \" SRC=\"$image\" ALT=\"align box\" ALIGN=LEFT></a>"; 

    echo "<a style=\"text-decoration:underline;\" onmouseover=\"this.style.textDecoration = 'none'\" onmouseout=\"this.style.textDecoration = 'underline'\" href=".$lol.">$title</a><br>"; 
    echo "<p style=\"color:#990000; padding-top:5px; font-weight:bold;\"> $19.90</p> <a style=\"font-weight:lighter;\" href=".$lol.">More info<img style=\"padding-left:3px;\" src=\"http://localhost/famfamfam.png\"></a> 
</td>"; 
    if ($i++ % 2 == 0) { 
     echo "</tr><tr><td style=\"padding-right:20px;\" colspan=\"1\"><hr style=\"width:250px;\" /></td> <td style=\"padding-right: 20px; padding-left: 30px;\" colspan=\"1\"><hr style=\"width:250px;\" /></td></tr>"; 
    } 
    $j++; 
} 
0

난 당신이 첫 번째 쿼리를 필요가 있다고 생각하지 않습니다. 나는 당신이 떠날 수 있다고 생각합니다 :

// use instead of your second query... notice the addition of post_title. 
$sql2 = "SELECT guid, post_title FROM `wordpress`.`wp_posts` ". 
    "WHERE post_mime_type ='image/png' ORDER BY `wp_posts`.`ID`"; 

// later! (you've done stuff in the meanwhile!) 
// note: assoc is used because it is technically more appropriate 
while($row2 = mysql_fetch_assoc($result2)){ 

    $image = $row2['guid']; 
    // this will always be the title which goes with the guid! 
    $title = $row2['post_title']; 
+0

@Muhammad Yasir 불행히도 그것은 작동하지 않습니다. 위의 내용을 업데이트합니다. 시도해 줘서 고마워 :) – sg552

관련 문제