Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Creating simple php dynamic html elements
<?php
function create_link( $title, $url, $attributes=array()){
$o = "<a ";
$o .= "href='".$url."'";
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
$o .= '>' . $title . '</a>';
return $o;
}
function create_list($data=array(), $type="ol", $attributes=array()){
$o = "<$type ";
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
$o .= '>';
foreach($data as $list){
$o .= '<li>' . $list . '</li>';
}
$o .= "</$type>";
return $o;
}
function create_image( $src, $description, $attributes=array()){
$o = "<img ";
$o .= "src='".$src."'";
$o .= "alt='".$description."'";
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
return $o;
}
function create_table($headers=array(), $rows=array(), $attributes=array()){
$headersCount = count($headers);
$o ="<table ";
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
$o .= '>';
$o .= '<thead><tr>';
foreach($headers as $heading){
$o.= '<th>'.$heading.'</th>';
}
$o .= '</tr></thead>';
$o.='<tbody>';
foreach($rows as $row){
$o.='<tr>';
for($i=0; $i<=$headersCount-1; $i++){
$o.=" <td>".$row[$i]."</td>";
}
$o.='</tr>';
}
$o.='</tbody>';
return $o;
}
function form_open_widget($action="", $method="post", $fileUpload=false, $attributes=array()){
$o = "<form ";
$o .= "action='".$action."' ";
$o .= "method='".$method."' ";
if($fileUpload){
$o .= "enctype='multipart/form-data' ";
}
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
$o .= '>';
echo $o;
}
function form_close_widget(){
echo "</form>";
}
function form_input( $name="", $type="text", $value="", $attributes=array()){
$o = "<input ";
$o .= "name='".$name."' ";
$o .= "type='".$type."' ";
$o .= "value='".$value."' ";
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
$o .= '>';
return $o;
}
function form_textarea( $name="", $text, $attributes=array()){
$o = "<textarea ";
$o .= "name='".$name."' ";
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
$o .= '>';
$o .= $text;
$o .= '</textarea>';
return $o;
}
function form_list( $name="", $select, $data, $attributes=array()){
$o = "<select ";
$o .= "name='".$name."' ";
if(!empty($attributes)){
foreach($attributes as $key =>$value){
$o .= "$key='".$value."' ";
}
}
$o .= '>';
foreach( $data as $item=>$value){
if($select == $item){
$o.='<option value="'.$item.'" selected="selected">'.$value.'</option>';
}else{
$o.='<option value="'.$item.'">'.$value.'</option>';
}
}
$o.= '</select>';
return $o;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment