Typecho is a simple and efficient open-source PHP framework, suitable for building various types of websites, including e-commerce platforms. In this article, we’ll discuss how to use PHP and Typecho to create a basic e-commerce platform, covering aspects such as database configuration, creating product tables, and customizing themes.
/** Database username */ define('__TYPECHO_DB_USER__', 'root'); /** Database password */ define('__TYPECHO_DB_PASSWORD__', ''); /** Database host */ define('__TYPECHO_DB_HOST__', 'localhost'); /** Database name */ define('__TYPECHO_DB_NAME__', 'typecho'); /** Database type */ define('__TYPECHO_DATABASE_TYPE__', 'mysql');
CREATE TABLE products ( id INT(11) AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255) NOT NULL, price DECIMAL(10,2) NOT NULL, description TEXT, image VARCHAR(255) );
This table includes fields for the product name, price, description, and image URL. You can further expand it based on your requirements.
Inside this folder, create a file named products.php, and add the following code:
<?php while($this->next()): ?> <div class="product"> <h3><?php $this->title(); ?></h3> <p><?php $this->content(); ?></p> <p>Price: <?php $this->fields->price(); ?></p> <?php if ($this->fields->image): ?> <img src="<?php echo $this->fields->image(); ?>" alt="<?php $this->title(); ?>" /> <?php endif; ?> </div> <?php endwhile; ?>
This code will display the product's title, content, price, and image. You can further customize it according to your needs.
title: Product List --- <?php $this->widget('Widget_Contents_Post_Recent', 'pageSize=10&type=page')->to($products); ?> <?php while($products->next()): ?> <div class="product"> <h3><a href="<?php $products->permalink(); ?>"><?php $products->title(); ?></a></h3> <p><?php $products->content(); ?></p> <p>Price: <?php $products->fields->price(); ?></p> <?php if ($products->fields->image): ?> <img src="<?php echo $products->fields->image(); ?>" alt="<?php $products->title(); ?>" /> <?php endif; ?> </div> <?php endwhile; ?>
This code will display the title, content, price, and image of the products, with links to their detail pages.
title: Product Detail --- <div class="product"> <h3><?php $this->title(); ?></h3> <p><?php $this->content(); ?></p> <p>Price: <?php $this->fields->price(); ?></p> <?php if ($this->fields->image): ?> <img src="<?php echo $this->fields->image(); ?>" alt="<?php $this->title(); ?>" /> <?php endif; ?> </div>