Jump to content
php.lv forumi

Forums: Attiecīgo forumu izvadīšana savā kategorijā


daGrevis

Recommended Posts

Sveiki,

 

Veidoju forumu, bet nesanāk gluži tā kā iecerēts. *blush*

 

Tabula - board__categories:

 

`id` int( 8 ) NOT NULL auto_increment,
`name` varchar( 255 ) NOT NULL,

PRIMARY KEY ( `id` )

 

Tabula - board__forums:

 

`id` int( 8 ) NOT NULL auto_increment,
`category_id` int( 8 ) NOT NULL,
`name` varchar( 255 ) NOT NULL,
`description` varchar( 255 ) NOT NULL,

PRIMARY KEY ( `id` )

 

Tagad gribu to visu smuki izvadīt, domāju, ka izvadās kategorijas nosaukums, pēc tam attiecīgie forumi, kas tai "pieder". Bet kas nesanāk ar pieprasījumu.

 

SELECT board__categories.name, board__forums.category_id, board__forums.name, board__forums.description
FROM board__categories, board__forums

 

print_r() izvada:

 

Array

(

[0] => Ziņas

[name] => iPoker.lv ziņas

[1] => 1

[category_id] => 1

[2] => iPoker.lv ziņas

[3] => Šajā sadaļā nonāk ziņas par iPoker.lv forumu.

[description] => Šajā sadaļā nonāk ziņas par iPoker.lv forumu.

)

Link to comment
Share on other sites

O! =) Pieprasījumu laikam sataisīju kā vajag! =) Bet tagad tikai, kā lai to pareizi izvada?

 

Pieprasījums:

 

SELECT board__categories.id, board__categories.name, board__forums.category_id, board__forums.name, board__forums.description
FROM board__categories, board__forums
WHERE board__categories.id = board__forums.category_id

 

print_r():

 

 

Array

(

[0] => 1

[id] => 1

[1] => Ziņas

[name] => iPoker.lv ziņas

[2] => 1

[category_id] => 1

[3] => iPoker.lv ziņas

[4] => Šajā sadaļā nonāk ziņas par iPoker.lv forumu.

[description] => Šajā sadaļā nonāk ziņas par iPoker.lv forumu.

)

Link to comment
Share on other sites

Krč vienkārši loģika:

$tmp = 0;
$res = mysql_query("SELECT `c`.`id`, `c`.`name` AS `cname`, `f`.`name`, `f`.`description` FROM `board__categories` AS `c` JOIN `board__forums` AS `f` ON `f`.`category_id` = `c`.`id` ORDER BY `c`.`id` ASC");
while($row = mysql_fetch_object($res)){
 if($row->id != $tmp) echo '<h1>',$row->cname,'</h1>'; // cita kategorija -> izvadam nosaukumu
 echo '<p>',$row->name,' <small>',$row->description,'</small></p>';
 $tmp = $row->id; // nākamā ieraksta kategorija tiks salīdzināta ar šīs id
}

Edited by marcis
Link to comment
Share on other sites

Tikko atradu bugu! xD Izvadot pietiekami daudz forumus veidojās nepareizs div'u izvadījums (interesants vārds). Ja nav daudz forumu, tad viss ir pareizi. Ja ir vairāk, tad lūk...

 

post-3513-004612200 1288379609_thumb.jpg

 

PHP, kas izvada visu:

 

while( $database = mysql_fetch_array( $query ) )
{

if( $database['id'] !== $temporary_id )
{

	echo "

		<div class=\"category\">

			<div class=\"name\">
				<h3>{$database['cname']}<h3>
			</div>

	";

}

echo '

	<div class="forum">
		<h4><a href="#">' . $database['name'] . '</a></h4>
		<p>' . parse__bb_codes( $database['description'] ) . '</p>
	</div>

';

if( $database['id'] === $temporary_id )
{

	echo "

		</div>
		<br />

	";

}


$temporary_id = $database['id'];

}

 

CSS (2min rakstīju, tāpēc ir tāds, kāds ir): http://paste.php.lv/b509bcde201024b4ef95e732a8b3559d?lang=php ;

Izvadītais HTML: http://paste.php.lv/d8b0e63bbda72acabccfed91423b41b5?lang=php .

Link to comment
Share on other sites

te mazliet nepareiza loģika

 

 

varētu kaut kā tā

 

$firstIteration = true;
while( $database = mysql_fetch_array( $query ) ) 
{ 

       if( $database['id'] !== $temporary_id ) 
       { 

			// aizver ieprieksejo kategoriju, ja nav pirma iteracija
			if(!$firstIteration)
			{
				echo " 

						</div> 
						<br /> 

				"; 
			}

			// tad atver jaunu kategoriju
               echo " 

                       <div class=\"category\"> 

                               <div class=\"name\"> 
                                       <h3>{$database['cname']}<h3> 
                               </div> 

               "; 

       } 

       echo ' 

               <div class="forum"> 
                       <h4><a href="#">' . $database['name'] . '</a></h4> 
                       <p>' . parse__bb_codes( $database['description'] ) . '</p> 
               </div> 

       '; 

	$firstIteration = false;
       $temporary_id = $database['id']; 

}

// aizver pedejo kategoriju
echo " 

						</div> 
						<br /> 

				"; 

Edited by php newbie
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...