Jump to content
PVM

PHP problem printing results from SQL database

Recommended Posts

First i would like to thank you for accepting me in this large community, I have learned a lot from this website, but now I have a problem that I am trying to solve for like a couple days so I think I really need your help guys :)

 

My problem is displaying results from SQL database to one XHTML file, here's the code, strings in code are mostly writent on my native language, if that is a problem i will easly translate it ofcourse...

 

This is my XHTML part:

<div id="content">


        <form action="ispis.php" method="POST" style="margin-top: 2%;float: left;margin:2%;padding:2%;height: 100%;border: solid thin">
            <label>Choose products group:</label><br/><br/>
            <select name="pd">
                <option value="komp">Racunari</option> <!-- Computers -->
                <option value="laps">Laptopovi</option> <!-- Laptops -->
                <option value="moni">Monitori</option>  <!-- Monitors -->
                </optgroup>
            </select>
              <input type="button" name="sd" value="Go"/>
        </form>

    </div>

This is the PHP part writen in "ispis.php"

<?php
include("admin/db_config.php");

$pd=$_POST["pd"];

if ($pd=="komp") {
    $sql = "SELECT * FROM racunari";
    $result = $connection->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        $output = "";
        while ($row = mysqli_fetch_array($result)) {
            $output .= "<b>Model:</b>" . $row['Model'] . "<br />";
            $output .= "<b>Opis:</b>" . $row['Opis'] . "<br />";
            $output .= "<b>Slika:</b>" . $row['Slika'] . "<br />";
            $output .= "<b>Cena:</b>" . $row['Cena'] . "<br />";
        }

        echo $output;
    } else {
        echo "Nema rezultata";
    }
}

elseif ($pd=="laps")
{
    $sql = "SELECT * FROM laptopovi";
    $result = $connection->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        $output = "";
        while ($row = mysqli_fetch_array($result)) {
            $output .= "<b>Model:</b>" . $row['Model'] . "<br />";
            $output .= "<b>Opis:</b>" . $row['Opis'] . "<br />";
            $output .= "<b>Slika:</b>" . $row['Slika'] . "<br />";
            $output .= "<b>Cena:</b>" . $row['Cena'] . "<br />";
        }

        echo $output;
    } else {
        echo "Nema rezultata";
    }
}

else
{
    $sql = "SELECT * FROM monitori";
    $result = $connection->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
        $output = "";
        while ($row = mysqli_fetch_array($result)) {
            $output .= "<b>Model:</b>" . $row['Model'] . "<br />";
            $output .= "<b>Opis:</b>" . $row['Opis'] . "<br />";
            $output .= "<b>Slika:</b>" . $row['Slika'] . "<br />";
            $output .= "<b>Cena:</b>" . $row['Cena'] . "<br />";
        }

        echo $output;
    } else {
        echo "Nema rezultata";
    }
}

$connection->close();
?>

This is my XHTML file and i would like to see results from SQL databases on this page:

http://www.upslike.net/imgdb/rezsss-d17d85.png

 

 

 

Share this post


Link to post
Share on other sites

The action on the form need to be the url of the target page which is where the script should be inside the container element that you want to hold the data.

Share this post


Link to post
Share on other sites

Ok man thanks, i have managed to solve this problem now i am dealing with some other stuff...

 

I need to output image from database, this is what happends when i try that, i only get path to the image:

 

http://i.imgur.com/A9Ew2Sf.png

 

This is my output code:

if ($result->num_rows > 0) {
    // output data of each row
    $output = "";
    while ($row = mysqli_fetch_array($result)) {
        $output .= '<table style="width: 45%;margin: 2%;"border="1">';
        $output.= '<tr>';
        $output.= '<td style="color: crimson">';
        $output .= "<b>Model:</b>" . $row['Model'] . "<br />";
        $output.= '</td>';
        $output.= '</tr>';
        $output.= '<tr>';
        $output.= '<td style="max-height: 120px;height: 100px;text-align: justify">';
        $output .= "<b>Opis: </b>" . $row['Opis'] . "<br />";
        $output.= '</td>';
        $output.= '</tr>';
        $output.= '<tr>';
        $output.= '<td style="height: 120px;max-height: 120px">';
        $output .= "<b>Picture:</b>" . $row['Slika'] . "<br />";                    //Picture is here!
        $output.= '</td>';
        $output.= '</tr>';
        $output.= '<tr>';
        $output.= '<td style="color: green">';
        $output .= "<b>Cena:</b>" . $row['Cena'] . "<br />";
        $output.= '</td>';
        $output.= '</tr>';
        $output .= '</table>';

    }

    echo $output;

Share this post


Link to post
Share on other sites

Try putting the path as the src attribute of an <img> element.

Share this post


Link to post
Share on other sites

I am trying that for like an 40 mins... can someone please write me that part of code where $row['Slika'] is my src of <img> tag ?

Share this post


Link to post
Share on other sites

replace this :

 $output .= "<b>Picture:</b>" . $row['Slika'] . "<br />"; 

with this :

 $output .= "<b>Picture:</b><img src='" . $row['Slika'] . "'><br />";
  • Like 1

Share this post


Link to post
Share on other sites

 

replace this :

 $output .= "<b>Picture:</b>" . $row['Slika'] . "<br />"; 

with this :

 $output .= "<b>Picture:</b><img src='" . $row['Slika'] . "'><br />";

Thank you man you saved me, best wishes !!!

Share this post


Link to post
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...