Jump to content
Sign in to follow this  
Trixx-

PHP Saīsinājums [Help @Sliede]

Recommended Posts

Sveiki visiem.

Gribētos vērsties pie sliedes, jo viņš man agrāk ļoti smuki aprakstīja kodu un gribēju pajautāt vai viņš vai kāds cits, kas labi saprot php varētu uzrakstīt šo kodu īsāk, ja ir iespējams un smukāk, lai nebūtu itkā putra?

$sql = "SELECT
            *
        FROM
            categories
        WHERE
            cat_id = " . mysqli_real_escape_string($conn, $_GET['cat_id']);
 
$result = mysqli_query($conn, $sql);
 
if(!$result)
{
    echo 'The category could not be displayed, please try again later.' . mysqli_error($conn);
}
else
{
    if(mysqli_num_rows($result) == 0)
    {
        echo 'This category does not exist.';
    }
    else
    {
        while($row = mysqli_fetch_array($result)){
            if(isset($_GET['cat_id'])){
                echo '<div class="category"><span style="border-bottom: 2px solid #E74C3C;">'.$row['cat_name'].'</span></div>';
            }
            elseif(isset($_GET['cat_id']) && isset($_GET['forum_id'])) {
                echo '<div class="category"><span style="border-bottom: 2px solid #E74C3C;">'.$row['forum_name'].'</span></div>';
            }
        }
        $sql = "SELECT  
                    forum_id,
                    forum_name,
                    forum_description,
                    forum_topics,
                    forum_posts,
                    cat_id
                FROM
                    forum
                WHERE
                    forum_id = '" . mysqli_real_escape_string($conn, $_GET['forum_id']) . "'
                ORDER BY
                    forum_id
                DESC";
         
        $result = mysqli_query($conn, $sql);
         
        if($result)
        {             
        echo '<div class="forum">
        	<div class="forum_body">';      
                while($row = mysqli_fetch_assoc($result))
                {
                    if($_GET['forum_id'] == $row['forum_id']){
                    	echo '';
                    }
                }
        echo '
        </div>
        </div>';
        }
        else {
            echo mysqli_error($conn);
        }
    }
}

 

Share this post


Link to post
Share on other sites
<?php

function showTopics($catId, $forumId) {
    $result = mysqli_query($conn, 'SELECT * FROM categories WHERE cat_id = ' . intval($catId));

    if (!$result) {
        return 'The category could not be displayed, please try again later.' . mysqli_error($conn);
    }

    if (mysqli_num_rows($result) === 0) {
        return 'This category does not exist.';
    }

    $return = '';
    while ($row = mysqli_fetch_array($result)) {
        $return .= '<div class="category"><span style="border-bottom: 2px solid #E74C3C;">' . $row['cat_name'] . '</span></div>';
    }


    $result = mysqli_query($conn, 'SELECT
        forum_id,
        forum_name,
        forum_description,
        forum_topics,
        forum_posts,
        cat_id
    FROM forum WHERE forum_id = ' . intval($forumId));

    if (!$result){
        return mysqli_error($conn);
    }

    $return .= '<div class="forum">
      <div class="forum_body">';
    while ($row = mysqli_fetch_assoc($result)) {
        echo '';
    }
    $return .=  '</div></div>';

    return $return;
}

if (isset($_GET['cat_id']) && isset($_GET['forum_id'])) {
    echo showTopics($_GET['cat_id'], $_GET['forum_id']);
} else {
    echo "No cat selected";
}

 

  • Upvote 1
  • Downvote 1

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.