Jump to content

vinsb

Members
  • Posts

    38
  • Joined

  • Last visited

  • Days Won

    1

Everything posted by vinsb

  1. Simply select image from database. Display that image on page and buttons 'Prev' 'next' and 'Rand' navigate to next and prev image from database. Here is the image of end result. I just don't know how to make it better yet.
  2. I trying to rewrite my code with prepared statements to preventing SQL injection. This code take image from mysql and display on page. There is also buttons for 'Prev' and 'Next' image. As I said I'm new in php and may be my code is a bit mess but at least is working so far. First I made this $qry = $con->prepare("SELECT * FROM images order by ? DESC"); $qry->bind_param('i', $id); $qry->execute(); $result = $qry->get_result(); $result->fetch_array(); Before it was like this $query = "select * from images order by id DESC"; $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con)); But I can't understand how to rewrite other part of code. $line = mysqli_fetch_array($result, MYSQL_BOTH); if (!$line) echo ''; $previd = -1; $currid = $line[0]; if (isset($_GET['id'])) { do { $currid = $line[0]; if ($currid == $_GET['id']) break; $previd = $currid; $line = mysqli_fetch_array($result, MYSQL_BOTH); } while ($line); } if ($line) { echo "<div id=\"picture\">"; echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$line['name']."\" /></a><br />"; echo "<div id=\"caption\">".$line['caption']."</div><br />"; } else echo "Empty!\n"; if ($previd > -1) echo '<a href="pics.php?id='.$previd.'" class="prev_pic"><span>prev</span></a>'; echo str_repeat(' ', 5); $line = mysqli_fetch_array($result, MYSQL_BOTH); $query = "select * from images order by RAND() LIMIT 1"; $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con)); while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){ echo '<a href="pics.php?id='.$row['id'].'"class="random">rand</a>'; } echo str_repeat(' ', 5); if ($line) echo '<a href="pics.php?id='.$line[0].'" class="next_pic"><span>next</span> </a><br /><br />';
  3. Thank's for answer! I'm sorry but I don't understand you. Can you clarify your answer a little?
  4. Hello, as I am new in php and absolutely new to mysqli_* prepared statements I need a little help. I'm trying to convert some code but I have troubles. $qry = $con->prepare("SELECT * FROM images order by ? DESC"); $qry->bind_param('i', $id); $qry->execute(); $result = $qry->get_result(); $result->fetch_array(); // I don't realy know what to do after this line $line = mysqli_fetch_array($result, MYSQL_BOTH); if (!$line) echo ''; $previd = -1; $currid = $line[0]; if (isset($_GET['id'])) { do { $currid = $line[0]; if ($currid == $_GET['id']) break; $previd = $currid; $line = mysqli_fetch_array($result, MYSQL_BOTH); } while ($line); } if ($line) { echo "<div id=\"picture\">"; echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$line['name']."\" /></a><br />"; echo "<div id=\"caption\">".$line['caption']."</div><br />"; } else echo "Empty!\n"; if ($previd > -1) echo '<a href="pics.php?id='.$previd.'" class="prev_pic"><span>prev</span></a>'; echo str_repeat(' ', 5); $line = mysqli_fetch_array($result, MYSQL_BOTH); $query = "select * from images order by RAND() LIMIT 1"; $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con)); while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){ echo '<a href="pics.php?id='.$row['id'].'"class="random">rand</a>'; } echo str_repeat(' ', 5); if ($line) echo '<a href="pics.php?id='.$line[0].'" class="next_pic"><span>next</span> </a><br /><br />'; So this also work but doesn't show me First and Last record of DB. Also I know that now is mixed style of mysqli_* but thats the problem.
  5. So I've made this but still not workin corectly. When I enter in catgory is empty? $cat_id = $_GET['cat_id']; $cat_id = mysqli_real_escape_string($con, $cat_id); $query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'"; $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con)); if (isset ($_GET['id'])) { $id = $_GET['id']; $prevSQL = mysqli_query($con,"SELECT id FROM images WHERE id < $id ORDER BY id DESC LIMIT 1") or die (mysqli_error($con)); $nextSQL = mysqli_query($con, "SELECT id FROM images WHERE id > $id ORDER BY id ASC LIMIT 1") or die (mysqli_error($con)); $prevobj=mysqli_fetch_object($prevSQL); $nextobj=mysqli_fetch_object($nextSQL); $pc = mysqli_fetch_object(mysqli_query($con, "SELECT COUNT(id) as pid FROM images WHERE id<$id ORDER BY id DESC")) or die (mysqli_error($con)); $nc = mysqli_fetch_object(mysqli_query($con, "SELECT COUNT(id) as nid FROM images WHERE id>$id ORDER BY id ASC")) or die (mysqli_error($con)); $prev=$pc->pid>0 ? '<a href="pic.php?cat_id='.$cat_id.'&id='.$prevobj->id.'">Prev</a> |' : ''; $next=$nc->nid>0 ? '<a href="pic.php?cat_id='.$cat_id.'&id='.$nextobj->id.'">Next</a>' : ''; $row = mysqli_fetch_array($result); echo "<div id=\"picture\">"; echo "<img src=\"upload/" . $row['name'] . "\" alt=\"\" /><br />"; echo $row['caption'] . "<br />"; echo "</p>"; echo $prev; echo $next; echo '</div>'; }
  6. Ok, thank's for that.
  7. In the href where I show categories or on every href?
  8. Thank's I gues...
  9. What I need to make buttons to work properly? Two more querys to database? Where to put them and to which table I must make them - images or cats?
  10. i thought that once I choose category I need only ID's from there. I still learning and don't know how exactly to do it. Can you help me?
  11. Hello, I trying to show categories and when user click on some category to load images only from this category. Here is what I have so far. The problem is that whenever I click on 'Next' or 'Prev' button the picture is the same. Doesn't change images. $q = mysqli_query($con,"select * from cats"); while ($res = mysqli_fetch_assoc($q)) { echo '<a href="pic.php?cat_id='. $res['cat_id'] .'">'.$res['cat_name'].'<br/>'; } ?> <hr> <?php $cat_id = $_GET['cat_id']; $cat_id = mysqli_real_escape_string($con, $cat_id); $query = "SELECT * FROM images JOIN cats ON images.img_category = cats.cat_id WHERE cats.cat_id = '$cat_id'"; $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con)); $line = mysqli_fetch_array($result, MYSQL_BOTH); if (!$line) echo ''; $previd = -1; $currid = $line[0]; if (isset($_GET['id'])) { do { $currid = $line[0]; if ($currid == $_GET['id']) break; $previd = $currid; $line = mysqli_fetch_array($result, MYSQL_BOTH); } while ($line); } if ($line) { echo "<div id=\"picture\">"; echo "<img style=\"width:100%;margin:0 auto;\" src=\"upload/".$line['name']."\" /></a><br />"; echo "<div id=\"caption\">".$line['caption']."</div><br />"; } else echo "There is no images!\n"; if ($previd > -1) echo '<a href="pic.php?cat_id='.$previd.'" class="prev_pic"><span>Prev</span></a>'; echo str_repeat(' ', 5); $line = mysqli_fetch_array($result, MYSQL_BOTH); $query = "select * from images order by RAND() LIMIT 1"; $result = mysqli_query($con, $query) or die("Query failed: " . mysqli_errno($con)); while ($row = mysqli_fetch_array($result, MYSQL_BOTH)){ echo '<a href="pic.php?cat_id='.$row['id'].'"class="random">Random</a>'; } echo str_repeat(' ', 5); if ($line) echo '<a href="pic.php?cat_id='.$line[0].'&id='.$line[0].'" class="next_pic"><span>Next</span> </a><br /><br />';
  12. So if I'm understand you correct I need something like this: id, image, name, filepath ---> after filepath I will make one more field 'categories' where I will save what is selected from <select> in form. Then I will make another table where I will have - id, name_of_categori.. Then whit where and join clause I'll point them. Hm is a little bit more for me since I'm beginner .. but I'm gues there is no other way to learn it Thank's for help!
  13. Hello, First I want to apologize if this post is in wrong place. So I started to learn php&mysql recently and decided to make one small web site. Is 2 pages - index.php and upload.php. So far I upload successfully image into folder and store name and path in database. Now I want to make categories. In my index page I will have few categories. So I've made <select> options on my upload form and one more row in database 'categoris' where to store which category is selected. My question is which way is beter to store categories? In separate table or as now in same table? Also when I click on desired category, how to show images only from this category?
×
×
  • Create New...

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.