0

「res_properties」というテーブルの内容を印刷して、ポートフォリオの概要を取得したいのですが、構文についてはわかりません (PHP は私を知っていますが、私は PHP を知りません...):

「res_properties」と呼ばれるテーブルと「location_id」と呼ばれる列があり、「res_location」と呼ばれる別のテーブルと「id」と「slug」と呼ばれる2つの列があり、列「slug」には人間が読める場所のコンテンツが含まれます

サンプルの「id」は「3」で「California」(「slug」の内容)、「id」は「4」で「Virginia」(「slug」の内容)などです。

印刷するときは、テーブル「res_location」をルックアップする必要があり、「location_id」が「id」と等しい場合は、「id」を「slug」のコンテンツに置き換えます (または、「slug」のコンテンツを「location_name」などの新しいフィールドに移動します) ' (25 文字) を入力し、そのフィールドを出力します。

これまでのところ、私のコードはこれであり、助けていただければ幸いです。どうもありがとう:

<html>
<head>
<title>Portfolio</title>
</head>
<link rel="stylesheet" type="text/css" href="../scripts/css/formate.css" />
<body>

<?php 
 // Connects to Database 
 mysql_connect("localhost","user","passw") or die(mysql_error()); 
 mysql_select_db("db_name") or die(mysql_error()); 
 $data = mysql_query("SELECT * FROM res_properties"); 
 $location = mysql_query("SELECT * FROM res_locations")

 or die(mysql_error()); 

 Print "<table border cellpadding=3>"; 
 Print "<br><h3>Portfolio</h3><p><br>"; 
   Print "<table border cellpadding=3>"; 
   Print "<tr align='left'><th width=130>Villa Name</th><th width=40>Beds</th><th width=40>Baths</th><th width=60>Sleeps</th><th width=40>Location</th><th width=40>Loc-ID</th><th width=300 >Site URL</th></tr>"; 

   while($info2 = mysql_fetch_array( $location )) 
   while($info = mysql_fetch_array( $data )) 

 { 

 Print "<tr>"; 
 Print "<td>".$info['ref_id'] . "</td> "; 
 Print "<td>".$info['bedrooms'] . " </td>"; 
 Print "<td>".$info['bathrooms'] . " </td>"; 
 Print "<td>".$info['max_occupants'] . " </td>"; 
 Print "<td>".$info2['slug'] . " </td>"; 
 Print "<td>".$info2['id'] . " </td>"; 

// here I want to print a clickable URL but some syntax error:
// Print "<td>" http://www.domain_name.com/'.$info['slug'] .'.html;

// when using the echo command it works fine like this, but doesn't output a clickable URL:
// echo 'http://www.domain_name.com/'.$info['slug'] .'.html' . "<br />"; 


 } 
 Print "</table>"; 
 ?> 

</body>
</html>
4

3 に答える 3

0

テーブル res_properties および res_locations の内容を印刷したいということは理解しています。テーブル res_properties には、特定の状態にあるプロパティが含まれています。出力では、プロパティとプロパティが配置されている州を印刷したいと考えています。これを行う最善の方法は、res_properties の列 location_id と res_locations の列 ID を使用して、単一の SQL ステートメントでテーブル res_properties と res_locationsを結合することです。

$data = mysql_query("SELECT * FROM res_properties t1 INNER JOIN res_locations t2 ON t1.location_id = t2.id");

join ステートメントを使用すると、mysql は単一のプロパティに対応するすべての情報を含む単一の結合テーブルを返します。$data を繰り返し、値を出力します。

print "<table>";
while($info = mysql_fetch_array( $data )) {
   print "<tr>"; 
   print "<td>" . $info['ref_id'] . "</td> "; 
   print "<td>" . $info['bedrooms'] . "</td>"; 
   print "<td>" . $info['bathrooms'] . "</td>"; 
   print "<td>" . $info['max_occupants'] . "</td>"; 
   print "<td>" . $info['slug'] . "</td>"; 
   print "<td>" . $info['id'] . "</td>";
   print "<td>http://www.domain_name.com/" . $info['slug'] . ".html</td>";
}
print "</table>";

PHP をよりよく理解するには、2 つの連続する while ループのセマンティクスについて考えてみてください。

while($info2 = mysql_fetch_array( $location )) 
while($info = mysql_fetch_array( $data )) 
{ 

また、構文エラーを回避するために、 printステートメントで適切な php 構文を使用するように注意してください。たとえば、プレーンな文字列値を出力するには、括弧が必要です:

print "hello world";

私のコードに構文エラーがなく、あなたの質問に答えたことを願っています。

于 2012-11-17T09:10:10.897 に答える
0

質問の本文を読むのは難しいですが、タイトルが正確であれば、これはあなたが望むものかもしれません.

SELECT IF(table.fieldA = table.fieldB, table.fieldC, NULL) FROM table;

形式はIF(条件、真なら出力、偽なら出力)

私の答えが正しい軌道に乗っているが、完全に正しくない場合、あなたが教えてくれたら、それを更新します。

于 2012-11-17T08:46:55.410 に答える
0

mysql で結合を使用すると、PHP スクリプトについて心配する必要がなくなります。次の sql クエリを使用してデータを取得します

$data = mysql_query("select
rl.id,
rl.slug
from res_location as rl
join res_properties as rp on rp.location_id=rl.id");

w3easystep.info で私のブログをフォローしてください

于 2012-11-17T08:50:42.770 に答える