|
|
|
| вообщем проблема такая, с помощью этого кода выводиться данные из таблицы recipes
---
if(!isset($_POST['add']))
$row=mysql_fetch_array(mysql_query("SELECT * FROM recipes WHERE id=".$_GET['id']));
$date = date("Y-m-d");
$image = "<img src=\"image/$row[img2]\" >";
print "<table wight=100%> <tr bgcolor=\"#FFFFFF\"><td valign=\'top'\ ><b>$row[caption]</b> / $date </td></tr></table>
<table wight=100%><tr><td>$image <br clear=\"left\"></td><td>$row[recipe] </td></tr></table>
";
---
сайт рецептов, к каждому рецепту привязаны как бы несколько изображений которые хранятся в images-recipes
структура recipes: id | group | ... | img2
структура images-recipes: id | id_recipes | img3
задача: чтобы $image выводил ряд изображений привязанных к id рецепта из таблицы images-recipes а все остальное брал из recipes.
помогите чайнику разобраться)) | |
|
|
|
|
|
|
|
для: werstalfranker
(12.03.2012 в 13:12)
| | Нужно сформировать и отправить дополнительный запрос к таблице images-recipes. У вас там несколько изображений на каждый рецепт? Тогда придется функцию mysql_fetch_array() выполнять несколько раз, лучше всего поместив её в цикл while(). Вот примерная схема
<?php
$query = "...";
$img = mysql_query($query);
if(!$img) exit("Ошибка - ".mysql_error());
if(mysql_num_rows($img))
{
while($image = mysql_fetch_array($img))
{
...
}
}
?>
|
| |
|
|
|
|
|
|
|
для: cheops
(12.03.2012 в 14:26)
| |
<html>
<head>
<title>Новости</title>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">google.load("jquery", "1");</script>
<script type="text/javascript" src="../js/labels.js"></script>
<link rel="stylesheet" type="text/css" href="../css/comments.css" />
<style type="text/css">
body { font:12px/1.3 Times New Roman, Sans-serif; }
form { width:380px;padding:0 90px 20px;margin:auto;}
div { clear:both;position:relative;margin:0 0 10px; }
label { cursor:pointer;display:block; }
input[type="text"] { width:300px;border:1px solid #999;padding:5px;-moz-border-radius:4px; }
input[type="text"]:focus { border-color:#777; }
input[type="password"] { width:300px;border:1px solid #999;padding:5px;-moz-border-radius:4px; }
input[type="password"]:focus { border-color:#777; }
textarea { width:300px;border:1px solid #999;padding:5px;-moz-border-radius:4px; }
textarea:focus { border-color:#777; }
input[type="submit"] { cursor:pointer;border:1px solid #999;padding:5px;-moz-border-radius:4px;background:#eee; }
input[type="submit"]:hover,
input[type="submit"]:focus { border-color:#333;background:#ddd; }
input[type="submit"]:active{ margin-top:1px; }
input[type="button"] { cursor:pointer;border:1px solid #999;padding:5px;-moz-border-radius:4px;background:#eee; }
input[type="button"]:hover,
input[type="button"]:focus { border-color:#333;background:#ddd; }
input[type="button"]:active{ margin-top:1px; }
</style>
</head>
<body>
<script>
function splash()
{
if (document.myForm.user.value =='')
{
alert ("Заполните имя пользователя!");
return false;
}
if (document.myForm.msg.value =='')
{
alert ("Заполните текст сообщения!");
return false;
}
return true;
}
</script>
<?php
include("config.php");
include("config2.php");
include("config3.php");
mysql_connect($hostname, $username, $password)or die("cant connect to database");
mysql_select_db("$dbName") or die("Не могу выбрать базу данных ");
//скрипт для формы
if(isset($_POST['add'])) {
$id_recipes = $_GET['id'];//id новости
$user = $_POST['user'];
$ip = $_SERVER['REMOTE_ADDR'];
$msg = $_POST['msg'];
$date = date("Y-m-d H:i:s");
$sql = "INSERT INTO `comments-recipes` (`id`, `id_recipes`, `user`, `ip`, `date`, `msg`) VALUES ('', '$id_recipes', '$user', '$ip', '$date', '$msg');";
}
if (mysql_query($sql))
{
header('Location: /recipes/single.php?id=' . $id_recipes);
exit();
}
if(!isset($_POST['add']))
$row=mysql_fetch_array(mysql_query("SELECT * FROM recipes WHERE id=".$_GET['id']));
$date = date("Y-m-d");
print "<table wight=100%> <tr bgcolor=\"#FFFFFF\"><td valign=\'top'\ ><b>$row[caption]</b> / $date </td></tr></table>
<table wight=100%><tr><td><br clear=\"left\"></td><td>$row[recipe] </td></tr></table>
";
?>
<?php
$query = "SELECT img3 FROM images-recipes WHERE id=".$_GET['id'];
$img = mysql_query($query);
if(!$img) exit("Ошибка - ".mysql_error());
if(mysql_num_rows($img))
{
while($image = mysql_fetch_array($img))
{
$image = "<img src=\"image/$row[img3]\" >";
echo ''.image.'';
}
}
?>
<form method="POST" id="info" name="myForm" onSubmit="return splash();">
<div id="login-wrap" class="slider">
<label for="login">Имя/Логин</label><input type="text" name="user"><br />
Имя и сообщение не должны содержать знак '</div>
<div><textarea name="msg" style="width: 300px;"></textarea></div>
<input type="submit" name="add" value="Отправить">
</form>
</body>
</html>
<?php
$id = $_GET['id'];//берем id новости
$sql = "SELECT * FROM `comments-recipes` WHERE `id_recipes`=' ".$id." ' ORDER BY date DESC";
$res = mysql_query($sql);
$number = 0; // переменная для нумерации, присваиваем 0
while($row = mysql_fetch_array($res)) {
$number = $number + 1; //увеличиваем на единицу(нумерация с единицы)
$id = $row['id'] + 1;
echo '<table class="comments"><tr><td class="user"><font size="4">'.$row['user'].'</font><br /><font size="2" color=#bebebe>#'.$number.'</font></td></tr><tr><td class="msg">'.$row['msg'].'</td></tr><tr><td align=right valign="bottom"><i><font size=1>'.$row['date'].' (<abbr title="по московскому времени GMT +03:00">MSK</abbr>)</font></i></td></tr></table>';
}
mysql_close();
?>
|
так? | |
|
|
|
|
|
|
|
для: werstalfranker
(13.03.2012 в 09:17)
| | Если работает так, как ожидалось, то все верно :) | |
|
|
|
|
|
|
|
для: cheops
(13.03.2012 в 12:58)
| | нет не работает, говорит синтаксическая ошибка( | |
|
|
|