Echo html code from php to html - php

I'm working on my own project for a music site and i'm blocked on a situation. Right now i'm working on the playlist and i want to echo from php(i'm working with odbc connection) on html, but it's not putting all the data, only the first row, and i want all the data from database to be echo in the playlist.
I tried to make an echo statement in php which to send row by row with html code.
This is my PHP:
<?php
error_reporting(6);
include("connect.php");
include("checklogin.php");
$username = $_SESSION['username'];
//echo $username;
$sql="select name from [YourTube].[dbo].[yt_url]
where username='$user_check'";
$rs=odbc_exec($conn,$sql);
//echo $sql;
while(odbc_fetch_row($rs)){
$name = odbc_result($rs, 1);
//print("$name\n");
}
?>
And i have tried to put something like this in php>>
<?php if($rs) { ?>
<li class="list_item selected">
<div class="title" action="../php/songs.php"><?php echo
odbc_result($rs, 1)?></div>
</li>
<?php } ?>
And the result is just the first row from database, but if i print $name it's showing all the rows.
The html code for playlist:
<div class="list_wrapper">
<ul class="list">
<li class="list_item selected">
<div class="info">
<div class="title" action="../php/songs.php"><?php echo
odbc_result($rs, 1)?></div>
</div>
</li>
</ul>
</div>
And like i said, this code on html it's showing only one row in playlist, not all of them. And i'm thinking to do something like echo from php in html code for every row something like this:
<li class="list_item selected">
<div class="info">
<div class="title" action="../php/songs.php"><?php echo
odbc_result($rs, 1)?></div>
</div>
</li>
Can someone help me with this? I'm working on this thing for some days and i can't figure out how to do it.
Thank you in advance!
Bogdan

You have to apply while() inside if() like below:-
<?php if($rs) {
while(odbc_fetch_row($rs)){
?>
<li class="list_item selected">
<div class="title" action="../php/songs.php"><?php echo odbc_result($rs, 1)?></div>
</li>
<?php }
} ?>

Related

Can't Link <a href="#"> W/ PHP code

I tried to link $child['id'] but can't do it. Link is removed on line 13. Can anyone tell me correct way to generate category link on click?
<!-- fetch parent categories -->
<?php
while ($parent = mysqli_fetch_assoc($parentquery)) : ?>
<?php
$parent_id=$parent['cat_id'];?>
<!--fetch sub-categories-->
<?php
$sql2 = "SELECT * FROM categories WHERE cat_parent = '$parent_id'";
$child_query = $db->query($sql2); // database object
?>
<div class="col-menu col-md-3">
<h6 class="title"><?php echo $parent['cat_name'] ?></h6>
<div class="content">
<ul class="menu-col">
<?php while($child = mysqli_fetch_assoc($child_query)) : ?>
Now I want to link each category on the below. Loops works fine. I am seeing category names but no link. Please help
<li> <a href='#'>
<?php echo $child['cat_name']; ?></a></li>
<?php endwhile; ?>
</ul>
</div>
</div>
<?php endwhile; ?>
You need to pass it in href
<li>
<a href='<?php echo $child['link'];?'>
<?php echo $child['cat_name']; ?>
</a>
</li>
Try this if ur href is empty then you should add
<a href='"<?php echo $child['link'];?>"'> // double quotes
you should pass the id to the href,
<a href='<?=$child['id'];?>'>

MySQLi query in variable/function (that can be later used to echo content)

So I'm stuck with a problem. I'm creating some kind of "templated" page (no, I can't use Twig or anything like that) in PHP, that will later be incremented with actual content.
My main problem here is to echo links in navbar. I'm using MaterializeCSS, so in order to make the navbar responsive is to write the links twice inside different ULs. I can easily echo it one time, like this:
<?php
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$navstructure =
<<<HTML
<header>
<nav class="$primarycolor">
<div class="container">
<div class="nav-wrapper">
$sitename
<ul class="right hide-on-med-and-down">
HTML;
echo $navstructure;
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<li>' . $row['pagename'] . '</li>
' ;
}
}
echo "
</ul>
</div>
</div>
</nav>
</header>";
?>
This works with no errors. The problem is, I need that (according to MaterializeCSS specifications/documentation):
<nav>
<div class="nav-wrapper">
Logo
<i class="material-icons">menu</i>
<ul class="right hide-on-med-and-down">
// links
</ul>
<ul class="side-nav" id="mobile-demo">
// links again
</ul>
</div>
</nav>
So what I wanted to do was simple: Make a navbar.php and have the structure on a HEREDOC variable, and in the variable $navbarlinks I would print all the links I got from the first code I showed you. The problem for me is that putting the links in a variable instead of showing all the results will only print one, and I couldn't find a way to change this.
What should I do to have a $navbarlinks or navbarlinks() that prints all the mySQL results and work everywhere?
You want to just put it in an array for later use. You should also try to separate your PHP from your HTML, this might be a good start but still needs things like HTML escaping. Note in particular the use of alternative syntax and short echo tags.
<?php
$links = [];
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result) {
while($row = $result->fetch_assoc()) {
$links[] = array_map('htmlspecialchars', $row);
}
}
// PHP is finished now, here's the HTML
?>
<?php if (count($links)):?>
<header>
<nav class="<?=$primarycolor?>">
<div class="container">
<div class="nav-wrapper">
<?=$sitename?>
<ul class="right hide-on-med-and-down">
<?php foreach ($links as $link):?>
<li><?=$link['pagename']?></li>
<?php endforeach?>
</ul>
<ul class="side-nav" id="mobile-demo">
<?php foreach ($links as $link):?>
<li><?=$link['pagename']?></li>
<?php endforeach?>
</ul>
</div>
</div>
</nav>
</header>
<?php endif?>
no longer practicing php but as far as i can remember html/css elements don't have to be pass to a variable.. you can just end php before the css elements and open php again from your loop
<?php
$sql = "SELECT id, pagename, filename FROM NavbarLinks";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<HTML
<header>
<nav class="$primarycolor">
<div class="container">
<div class="nav-wrapper">
<?php=$sitename?>
<ul class="right hide-on-med-and-down">
<?php
while($row = $result->fetch_assoc()) {
echo '<li>' . $row['pagename'] . '</li>
' ;
}
?>
</ul>
</div>
</div>
</nav>
</header>";
<?php
}
?>
<!-- end -->

Storing HTML tags including PHP script in the database using Codeigniter

i'm trying to store html tag that includes php script.
This is for the navigation menu, i'm trying to display only the menu for a particular user role.
I'm storing it this way
public function edit_module(){
$id = $this->input->post('Module_ID', TRUE);
$update = $this->db->update('ref_modules', array('Module_Name'=>$this->input->post('Module_Name', TRUE),
'Module_Menu'=> htmlspecialchars($this->input->post('Module_Menu')),
'UpdatedBy'=>$this->session->userdata('user_id')), "Module_ID = '$id'");
if($update){
return TRUE;
}
}
For display
<!--Menu-->
<!--================================-->
<div id="mainnav-menu-wrap">
<div class="nano">
<div class="nano-content">
<ul id="mainnav-menu" class="list-group">
<!--Category name-->
<li class="list-header">Navigation</li>
<?php if($this->data['menu']): ?>
<?php foreach($this->data['menu'] as $row): ?>
<?php echo htmlspecialchars_decode($row->Module_Menu); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div> <!-- nano -->
</div><!-- mainnav-menu-wrap -->
<!--================================-->
<!--End menu-->
When i view the source, its fine but when i click the link, special characters are showing.
http://localhost/folderName/%3C?php%20echo%20base_url(%27citizens%27);%20?%3E
Here's a sample value that i am inserting
<li class="<?php echo ($title == 'Citizens' ? 'active-link' : '');?>">
<a href="<?php echo base_url('citizens'); ?>">
<i class="fa fa-user"></i>
<span class="menu-title">
<strong>Citizens</strong>
</span>
</a>
</li>
Any idea guys? Thanks in advance!
Inserting Html into database For Different user role is not the right way. You can use if Conditions To show your Menu for different User roles.

Display nav list of content is available with php

I've got this PHP code:
<div class="connect_wrap <?php echo $this->class; ?> block" <?php echo $this->cssID; ?>>
<?php if(!$this->empty): ?>
<?php foreach($this->entries as $entry): ?>
<div class="entry block <?php echo $entry->class; ?>">
<div class="tab">
<ul class="tabs">
<li class="tab-link current" data-tab="Kunden">Kunden</li>
<li class="tab-link" data-tab="Loesungen">Lösungen</li>
</ul>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM kunden WHERE partner=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Kunden" class="tab-content current">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/kunden-detail/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
<?php
$this->import('Database');
$pName = $entry->field('name')->value();
$result = \Database::getInstance()->prepare("SELECT * FROM solutions WHERE solution_provider=?")->execute($pName);
?>
<?php if($result->numRows):?>
<div id="Loesungen" class="tab-content">
<?php while($result->next()) { ?>
<div class="items">
<a href="{{env::url}}/synaptic-commerce-solution/<?php echo $result->alias; ?>">
<div class="logo">
<img src="<?php $objFile = \FilesModel::findByUuid($result->logo); echo $objFile->path;?>"width="180" height="135">
</div>
</a>
</div>
<?php } ?>
</div>
<?php endif;?>
</div>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php endif;?>
In that code, I've got two DB queries. My problem is, if there is no data for both queries, the div with the class "connect_wrap" should not be displayed.
How can I do that?
Thanks in advance.
do you want to check if the query has data in it and its not empty ? if so try num_rows it will return the number of rows that the query found/affected for example to check if th query returned one or more rows
if($result->num_rows >= 1) {
//do stuf
} else {
// no result found
}
Move the query execution up or preferably: not within the html but in a different file/class. Then add a check before the content_wrap div: if($kundenResult->num_rows >= 1 || $solutionsResult->num_rows >= 1). If you just keep the individual checks like you already have, it will only show the content_wrap div if either or both of the queries return rows of data.

Some HTML tags disappear when i use PHP

I'm trying to generate dynamic contents fill from mysql db
here is my php code:
<?php
include 'header.php';
error_reporting(1);
$user = "root";
$pass = "";
$dbName = "haimi";
mysql_connect('localhost', $user, $pass) or
die("Could not connect: " . mysql_error());
mysql_select_db($dbName);
$sql = "SELECT * FROM Projects ";
$result = mysql_query($sql);
?>
<?php
while ($line = mysql_fetch_array($result)) {
?>
<li class="filter" data-filter=".cat<?php echo $line['Category'];?>"><?php echo $line['Category'];?></li>
<?php
}
?>
The li displays correctly, but the following does not:
<div class="row projects m0">
<?php
while ($line = mysql_fetch_array($result)) { ?>
<div class="project mix catHouses">
<div class="tint"></div>
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?> data-
lightbox="project" data-title="Central Hospital (building)">
<img src="images/projects/".<?php echo
$line['ProjectImage1']; ?> alt="<?php echo $line['ProjectTitle'];?>"
class="projectImg"> </a>
<div class="projectDetails row m0">
<div class="fleft nameType">
<div class="row m0 projectName"><?php echo $line['ProjectTitle'];?></div>
<div class="row m0 projectType"><?php echo $line['ProjectType'];?></div>
</div>
<div class="fright projectIcons btn-group" role="group">
<a href="images/projects/<?php echo $line['ProjectImage1']; ?>" data-lightbox="project" data-title="Central Hospital (building)" class="btn btn-default">
<i class="fa fa-link"></i></a>
<i class="fa fa- search"></i>
</div>
</div>
</div>
<?php
}
?>
</div>
It data in the divs doesn't appear.
You're making a single call, but trying to loop through it twice. To do so, you need to reset the pointer back to the beginning:
//Add this after the first loop, but before the second
mysql_data_seek( $result, 0 );
The way you have it now, it's while($line = mysql_fetch_array($result)), but the second loop is never entered since it has already reached the end. Since the loop is ended, it never displays the contents.
Important Note
The mysql_* functions are deprecated, and is removed in PHP 5.5. You should use Mysqli or PDO. They have better protections against mysql injections (see Bobby Tables).
You have some HTML mistakes here:
<a href="images/projects/".<?php echo $line['ProjectImage1']; ?>
First, there is no need to use . operator (as you are in HTML, not PHP),
Also you shoud put your <?php ?> tag inside the href quotations, here is the correct code:
<a
href="images/projects/<?php echo $line['ProjectImage1']; ?>"
data-lightbox="project"
data-title="Central Hospital (building)"
>
<img
src="images/projects/<?php echo $line['ProjectImage1']; ?>"
alt="<?php echo $line['ProjectTitle']; ?>"
class="projectImg"
>
</a>
You will get older fast writing code like that ;)
How about this:
while ($line = mysql_fetch_array($result)) {
$category = $line['Category'];
echo <<< LOB
<li class="filter" data-filter="$category">$category</li>
LOB;
}
Like what Mr #matthew said
I was making a single call, and I was trying to loop through it twice.
The problem solved with this code before the while loop:
$result = mysql_query($sql);

Categories