2011-08-15 9 views
0

2つのクエリがあります。最初のクエリは製品タイトルを取得し、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

@ムハマドヤシル残念ながらそれは動作しません。上記の内容を更新します。しかし、試してくれてありがとう:) – sg552

関連する問題